gpt4 book ai didi

java - Hibernate Criteria Api 子查询

转载 作者:搜寻专家 更新时间:2023-11-01 01:57:16 25 4
gpt4 key购买 nike

我目前正在开展一个项目,将一些遗留的 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 以来“正确”的方法是什么仅简要介绍 criteria 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 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5902898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com