gpt4 book ai didi

java - 实体之间没有关系的 Hibernate Criteria 查询

转载 作者:太空宇宙 更新时间:2023-11-04 06:32:42 25 4
gpt4 key购买 nike

我有两个表:

A (x, y, z, date1, date2)
B (t, date1, date2)

我想执行这样的查询:

SELECT A.x, A.y, A.z, A.date1
FROM A, B
WHERE A.date1 < B.date1
AND (A.date2 is null or A.date2 <= B.date2 )
ORDER BY A.x DESC;

如何使用 Hibernate Criteria 做到这一点?

最佳答案

String           dateIdValue   = "...";
Class<?> mainClass = A.class;
Class<?> dateClass = B.class;

String colDate1 = "date1";
String colDate2 = "date2";
String colDateId = "id";

Property date1Prop = Property.forName(colDate1);
Property date2Prop = Property.forName(colDate2);

DetachedCriteria date1Criteria = DetachedCriteria.forClass(dateClass)
.setProjection(date1Prop)
.add(Restrictions.eq(colDateId, dateIdValue));
DetachedCriteria date2Criteria = DetachedCriteria.forClass(dateClass)
.setProjection(date2Prop)
.add(Restrictions.eq(colDateId, dateIdValue));

Criteria criteria = getSession().createCriteria(mainClass)
.add(date1Prop.lt(date1Criteria))
.add(Restrictions.or(date2Prop.isNull(), date2Prop.le(date2Criteria)))
.addOrder(Order.desc("x"));

/*
SELECT A.*
FROM A, P
WHERE A.date1 < P.date1
AND (A.date2 is null or A.date2 <= P.date2 )
ORDER BY A.x DESC;
*/

关于java - 实体之间没有关系的 Hibernate Criteria 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25893886/

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