我试图转换这个 JPQL 查询;
SELECT s FROM QuestionSet s JOIN s.questions q WHERE q.appointedRepetition.date < :tomorrow
与其标准 api 等效,这是我到目前为止所拥有的:
DateTime tomorrow = DateTime.now().plusDays(1).withTime(0,0,0,0);
CriteriaBuilder criteriaBuilder = JPA.em().getCriteriaBuilder();
CriteriaQuery<QuestionSet> query = criteriaBuilder.createQuery(QuestionSet.class);
Root<QuestionSet> root = query.from(QuestionSet.class);
Join<QuestionSet, Question> questionJoin = root.join("questions");
Predicate ownerCondition = criteriaBuilder.equal(root.get("owner"), owner);
Predicate dateCondition = criteriaBuilder.lessThan(questionJoin.<DateTime>get("appointedRepetition.date"), tomorrow);
query.where(criteriaBuilder.and(ownerCondition, dateCondition));
List<QuestionSet> result = JPA.em().createQuery(query).getResultList();
return result;
但我得到
play.api.Application$$anon$1: Execution exception[[IllegalArgumentException: Unable to resolve attribute [appointedRepetition.date] against path [null]]]
查看如何将带有子查询的 JPQL 转换为等效的 Criteria API?我在标准 api 部分有几乎相同的代码。
@Entity
@SequenceGenerator(name = "wordlist_seq", sequenceName = "wordlist_seq")
public class QuestionSet {
@OneToMany(cascade = CascadeType.ALL)
private List<Question> questions;
...
}
@Entity
@SequenceGenerator(name = "question_seq", sequenceName = "question_seq")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Question{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "question_seq")
private Long id;
...
}
@OneToOne(cascade = CascadeType.ALL)
private AppointedRepetition appointedRepetition;