[英]How do you create a Spring JPA repository findBy query using a property that contains a keyword?
Here is a simplified example of my problem.这是我的问题的简化示例。 I have this repository and entity class.
我有这个存储库和实体类。
public interface ThingRepository extends JpaRepository<ThingEntity, Long> {
ThingEntity findByFooInAndBar(String fooIn, String bar);
}
@Entity
public class ThingEntity {
@Column(name="FOO_IN", nullable=false, length=1)
private String fooIn;
public String getFooIn() {
return fooIn;
}
public setFooIn(String fooIn) {
this.fooIn = fooIn;
}
/* not including bar property for brevity's sake */
}
Spring is throwing the following exception. Spring 抛出以下异常。
org.springframework.data.mapping.PropertyReferenceException: No property foo found for type ThingEntity!
It looks like Spring is taking the method findByFooInAndBar
and thinks that foo
is my property name and in
is a keyword for matching values within a collection.看起来 Spring 正在采用
findByFooInAndBar
方法并认为foo
是我的属性名称,而in
是用于匹配集合中值的关键字。
How do I get it to understand that the property name is fooIn
, not foo
?我如何理解属性名称是
fooIn
,而不是foo
?
To overcome this problem, I've defined the query manually using the @Query
annotation.为了解决这个问题,我使用
@Query
注释手动定义了查询。 I'll happily accept anyone else's answer if they find a solution that doesn't require a manual query.如果他们找到不需要手动查询的解决方案,我会很乐意接受其他人的回答。
public interface ThingRepository extends JpaRepository<ThingEntity, Long> {
@Query("SELECT t FROM Thing t WHERE t.fooIn = ?1 AND t.bar = ?2")
ThingEntity findByFooInAndBar(String fooIn, String bar);
}
Spring is parsing 'In' in your method to create the query. Spring 正在您的方法中解析“In”以创建查询。 Check the link for create your query: you should change the name of the variable
fooIn
to fooin
or something like that...检查创建查询的链接:您应该将变量
fooIn
的名称更改为fooin
或类似的名称...
I observed the code kindly and notice your return is not a list
but an object
.我友好地观察了代码,并注意到您的返回不是一个
list
而是一个object
。
Change that part to List<ThingEntity>
.将该部分更改为
List<ThingEntity>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.