java – Hibernate Criteria Api子查询

我目前正在开发一个项目,将一些遗留的jdbc select语句转移到使用Hibernate及其标准api.

两个相关的表列和SQL查询如下所示:

-QUERIES-
primaryId

-QUERYDETAILS-
primaryId
linkedQueryId -> Foreign key references queries.primaryId
value1
value2

select * 
from queries q 
where q.primaryId not in (SELECT qd.linkedQueryId 
                          FROM querydetails qd 
                          WHERE (qd.value1 LIKE 'PROMPT%' 
                                 OR qd.value2 LIKE 'PROMPT%'));

我的实体关系如下:

@Table("queries")
public class QueryEntity{

    @Id
    @Column
    private Long primaryId;
    @OneToMany(targetEntity = QueryDetailEntity.class, mappedBy = "query", fetch = FetchType.EAGER)
    private Set<QueryDetailEntities> queryDetails;

    //..getters/setters..

}

@Entity
@Table(name = "queryDetails")
public class QueryDetailEntity {
    @Id
    @Column
    private Long primaryId;

    @ManyToOne(targetEntity = QueryEntity.class)
    private QueryEntity query;

@Column(name="value1")
private String value1;

@Column(name="value2")
private String value2;

    //..getters/setters..

}

我试图以这种方式使用标准api:

Criteria crit = sessionFactory.getCurrentSession().createCriteria(QueryEntity.class);

DetachedCriteria subQuery = DetachedCriteria.forClass(QueryDetailEntity.class);
LogicalExpression hasPrompt = Restrictions.or(Restrictions.ilike("value1", "PROMPT%"),
        Restrictions.ilike("value2", "PROMPT%"));
subQuery.add(hasPrompt);
Criterion subQueryCrit = Subqueries.notIn("queryDetails", subQuery);
crit.add(subQueryCrit);

List<QueryMainEntity> entities = (List<QueryMainEntity>) crit.list();
System.out.println("# of results = " + entities.size());

我在看起来像的crit.list()行上得到一个NullPointerException

Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:362)
    at org.hibernate.criterion.SubqueryExpression.createAndSetInnerQuery(SubqueryExpression.java:153)
    at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:69)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:114)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:83)
    at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1687)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)

现在,我认为我非常安全地说我正在使用Criteria Api / Detached Query Api,但我不确定“正确”的方法是什么,因为Hibernate Docs只是简单地覆盖了标准api子查询.

我意识到这是一个很长的问题,但我认为它似乎把问题的所有相关方面(查询我试图通过Criteria API,表,实体来表示).

最佳答案
试一试:

DetachedCriteria d = DetachedCriteria.forClass(QueryDetailEntity.class, "qd");
d.setProjection(Projections.projectionList().add(Projections.property("qd.query")));
d.add(Restrictions.or(Restrictions.like("qd.value1", "PROMPT%"), Restrictions.like("qd.value2", "PROMPT%")));

criteria = session.createCriteria(QueryEntity.class, "q");
criteria.add(Subqueries.propertyNotIn("q.primaryId", d));
criteria.list();

以下是属性名称,而不是列名:

qd.query
qd.value1
qd.value2
q.primaryId

作为旁注,如果这不是动态生成的查询,您是否考虑过使用HQL?

转载注明原文:java – Hibernate Criteria Api子查询 - 代码日志