我试图转换这个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]]]
查看How to convert a JPQL with subquery to Criteria API equivalent?我在条件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;
答案 0 :(得分:2)
您需要另一个联接,但我不能保证它会起作用,因为实体定义要么缺失要么不完整,并且并非所有关系都按照评论中的说明进行定义。无论如何,我会尝试这个:
Join<Question, AppointedRepetition> repetition = questionJoin.join("appointedRepetition");
Predicate dateCondition = criteriaBuilder.lessThan(repetition.get("date"), tomorrow);
顺便说一下,我看到你正在使用joda的DateTime。我从未在JPA CriteriaBuilder中使用它,所以我不能保证它有效。