如何使用包含关键字的属性创建 Spring JPA 存储库 findBy 查询? [英] How do you create a Spring JPA repository findBy query using a property that contains a keyword?

查看:34
本文介绍了如何使用包含关键字的属性创建 Spring JPA 存储库 findBy 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题的一个简化示例.我有这个存储库和实体类.

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 抛出以下异常.

Spring is throwing the following exception.

org.springframework.data.mapping.PropertyReferenceException: No property foo found for type ThingEntity!

看起来 Spring 正在采用方法 findByFooInAndBar 并认为 foo 是我的属性名称,而 in 是用于匹配内部值的关键字一个集合.

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.

我如何理解属性名称是 fooIn,而不是 foo?

How do I get it to understand that the property name is fooIn, not foo?

推荐答案

为了解决这个问题,我使用 @Query 注释手动定义了查询.如果其他人找到不需要手动查询的解决方案,我会很乐意接受他们的回答.

To overcome this problem, I've defined the query manually using the @Query annotation. 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 JPA 存储库 findBy 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆