属性名称包含特殊单词的 Spring Data JPA 存储库方法 [英] Spring Data JPA repository method with property name containing a special word
问题描述
例如,我有一个具有以下属性的实体:
@Entity公共类 MyObj {公共字符串名称OrId;}
然后我继续创建存储库
public interface MyObjRepository extends JpaRepository{MyObj findByNameOrId(String nameOrId);}
这失败了,因为似乎 Spring 将方法名称标记为 findBy
Name
Or
Id
,而不是findBy
nameOrId
.
由此产生的错误是name"属性未找到(或类似).
有没有办法转义方法语法中的特殊词(Or"、And"等)?重命名列以不包含这些词不是一种选择.
一种解决方法是重命名 Java 字段以避免使用特殊词,同时使用 @Column
注释保留列名.
>
@Column(name = "nameOrId")公共字符串名称Id;
相应地重命名 JPA 方法.
MyObj findByNameId(String nameOrId);
For example, I have an entity with the following attribute:
@Entity
public class MyObj {
public String nameOrId;
}
I then proceed to create the repository
public interface MyObjRepository extends JpaRepository<MyObj, String> {
MyObj findByNameOrId(String nameOrId);
}
This fails, because it seems that Spring tokenizes the method name as findBy
Name
Or
Id
, instead of findBy
nameOrId
.
The resulting error is "name" property not found (or similar).
Is there a way to escape the special words ("Or", "And", etc) in the method syntax? Renaming the column to not include those words is not an option.
A workaround is to rename the Java field to avoid special words, while preserving the column name using the @Column
annotation.
@Column(name = "nameOrId")
public String nameId;
The JPA method is renamed accordingly.
MyObj findByNameId(String nameOrId);
这篇关于属性名称包含特殊单词的 Spring Data JPA 存储库方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!