使用Criterion的Hibernate中的嵌套查询

编程入门 行业动态 更新时间:2024-10-19 00:26:10
本文介绍了使用Criterion的Hibernate中的嵌套查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个下面的查询,我必须从临时表中选择由子查询创建的行。

从x中选择x,y,x 从some_table中选择x,y,z,其中x在x1和x2之间)其中y喜欢'y1'按z排序by desc

我必须使用Criteria从数据库中获取结果

我已经经历了使用标准和分离标准处理子查询的几个示例和文档。我已经使用分离查询,但它不服务的目的或我缺少的东西。

我使用下面的代码

DetachedCriteria subCriteria = DetachedCriteria.forClass(SomeClass.class) .add(Restrictions.between(x,x1,x2) ) .setProjection(Projections.projectionList() .add(Projections.property(x)) .add(Projections.property(y))。 add(Projections.property(z)); List< Object []> results = session .createCriteria(Program.class)。 exists(subCriteria)) .add(Restrictions.like(y,y1)) .addOrder(Order.desc(z))list();

HQL或JPQL都不支持派生表表达式。你可以使用sub - 选择或选择,但这是很多。 / p>

这次你需要使用本地查询,这实际上是正确的。当你想获取实体而不是投影时,HQL / JPQL最有用。

I have a following query where I have to select rows from temporary table created by subquery.

select x, y, x from (select x, y, z from some_table where x between x1 and x2) where y like 'y1' order by z by desc

I have to use Criteria for fetching result from database

I have gone through several examples and documentation for handling subqueries using criteria and detached criteria. I have used Detached query but it is not serving the purpose or I am missing something.

I have used following code

DetachedCriteria subCriteria = DetachedCriteria.forClass(SomeClass.class) .add(Restrictions.between("x","x1","x2")) .setProjection(Projections.projectionList() .add(Projections.property("x")) .add(Projections.property("y")) .add(Projections.property("z")); List<Object[]> results = session .createCriteria(Program.class) .add(Subqueries.exists(subCriteria)) .add(Restrictions.like("y", "y1")) .addOrder(Order.desc("z")).list();

解决方案

Neither HQL or JPQL support "Derived Table Expressions". You can use sub-selects or in-selects but that's much it.

You need to use a native query this time and that's actually the right thing to do. HQL/JPQL are mostly useful when you want to fetch Entities not Projections.

更多推荐

使用Criterion的Hibernate中的嵌套查询

本文发布于:2023-10-23 05:39:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519955.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   Criterion   Hibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!