如何使用JPA标准基于EmbeddedId的字段进行查询?

问题描述 投票:0回答:1

我花了最后几个小时来解决这个问题,主要是因为我发现某人已经完成了搜索,但是我找不到适合我的答案。

这是我的代码向我可以公开发布的即时翻译。

@Entity @Table(name="result")
public class Result implements Serializable {
    @Embeddable
    public static class ResultPK implements Serializable {
        @Column(name="result_date", nullable=false)
        @Type(type="com.example.HibernateUTC$LocalDateType") // <- UserType
        public LocalDate resultDate;
        @Column(name="name", nullable=false)
        public String name;
        @Column(name="category", nullable=false)
        public String category;
        public ResultPK() {}
        public ResultPK(Date resultDate, String name, String category) {
            this.resultDate = resultDate;
            this.name = name;
            this.category = category;
        }
        // ...more code for hashCode/equals, no setters or getters...
    }

    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name="resultDate", column=@Column(name="result_date", nullable = false)),
            @AttributeOverride(name="name", column=@Column(name="name", nullable=false)),
            @AttributeOverride(name="category", column=@Column(name="category", nullable = false)),
            })
    private ResultPK resultId;
    @Column(name="r_square")
    private Double rSq;
    @Column(name="p_value")
    private pValue;

    // ... more code for other fields, setters, getters, but nothing else; vanilla pojo...
}

我有一个DAO,其中隐藏了查询;我要调用的方法是这个

@Repository("resultDAO")
public class ResultDAOImpl extends AbstractBaseDAO<Result> implements ResultDAO {
    // boilerplate for intializing base class and other queries
    @Override
    public List<Result> findDateRange(String category, String name, LocalDate begDate, LocalDate endDate) {
        EntityManager em = entityManagerFactory.createEntityManager();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Result> q = cb.createQuery(Result.class);
        Root<Result> root = q.from(Result.class);
        Predicate catMatch = cb.equal(root.get("resultId.category"), category);
        Predicate nameMatch = cb.equal(root.get("resultId.name"), name);
        Predicate dateRange = cb.between(root.get("resultId.resultDate"), begDate, endDate);
        q.select(root).where(cb.and(catMatch, nameMatch, dateRange));
        return em.createQuery(q).getResultList();
    }
}

[当我尝试运行执行该查询的代码时,出现错误

Exception in thread "main" java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [resultId.category] on this ManagedType [com.example.Result]

[我发现的一些类似问题使它看起来像我需要在查询中使用resultPKResultPK。我已经尝试过了,没有喜悦。我不知道如何在查询键中指定字段,或者是否需要与此完全不同的东西。我真的需要一个线索...

我正在使用Spring 4.3.8.RELEASE和Hibernate 4.3.11.Final,Java 8(因此使用UserType处理LocalDate)。

已编辑,以纠正我在抄写实际代码时的某些不一致之处。

java criteria-api
1个回答
0
投票

您应该以这种方式修改谓词:

 Predicate catMatch = cb.equal(root.get("resultId").get("category"), category);
 Predicate nameMatch = cb.equal(root.get("resultId").get("name"), name);
 Predicate dateRange = cb.between(root.get("resultId").get("resultDate"), begDate, endDate);

通过这种方式,无需在cb.and语句内使用where。您可以使用

使代码短一些
q.select(root).where(catMatch, nameMatch, dateRange);
© www.soinside.com 2019 - 2024. All rights reserved.