Hibernate Criteria API 和标量子查询

发布于 2024-08-21 18:19:35 字数 943 浏览 11 评论 0原文

我想将 HQL 查询转换为 Criteria API,但我不知道是否可以使用 Criteria 编写相同的内容。

HQL 看起来像这样:

select distinct new DataTransferObject(property1, property2, ..., (select NVL(property3, null) from Table1 where property3 in elements(...) and ... ), property4, ..., (select .....), ...)
from Table2 as table2
left join table2.property5 as property5
(... more left joins ...)
where
.....

我开始像这样编写我的标准:

getSession().createCriteria(Table2.class, "table2")
.createAlias("table2.property5", "property5")
(...more createAlias...)
.add(Expression/Restriction
....

然后我创建了一个 ProjectionList,其中包含除那些(选择...)之外的所有构造函数参数。

最后:

criteria.setProjection( projectionList).setResultTransformer( Transformers.aliasToBean(DataTransferObject.class) ) .list()

我的问题是如何将这些(选择...)添加到projectionList ????

我试图实现的是在 select 子句(标量子查询)中创建子查询,而不是在 where 子句中,而是使用 Criteria API

I want to translate a HQL query to Criteria API but I don't know if it's possible to write the same thing with Criteria.

The HQL looks like this:

select distinct new DataTransferObject(property1, property2, ..., (select NVL(property3, null) from Table1 where property3 in elements(...) and ... ), property4, ..., (select .....), ...)
from Table2 as table2
left join table2.property5 as property5
(... more left joins ...)
where
.....

I started to write my Criteria like this:

getSession().createCriteria(Table2.class, "table2")
.createAlias("table2.property5", "property5")
(...more createAlias...)
.add(Expression/Restriction
....

I then created a ProjectionList with all the constructor arguments except those (select ...).

and at the end:

criteria.setProjection( projectionList).setResultTransformer( Transformers.aliasToBean(DataTransferObject.class) ) .list()

My question is how to add those (select ...) to the projectionList ????

What I try to achieve is to make a subquery in the select clause (scalar subqueries), not in the where clause, but using Criteria API

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文