HQL新对象(..)中的新列表(..) [英] HQL new List(..) within new Object(..)

查看:83
本文介绍了HQL新对象(..)中的新列表(..)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求在哪里在HQL查询中创建一个新对象.新的Object构造函数中要提供的参数之一是一些其他对象的列表.

I have requirement where in I would like to create a new object within HQL query. And one of the parameters to be provided in the new Object constructor is a list of some other objects.

例如:

SELECT new Object1(a.id, new List(SELECT b FROM table2 AS b WHERE b.id>0)) FROM table1 AS a;

所以我将得到类型为Object1的对象,该对象具有从另一个表中检索的列表.

So I would be getting an object of type Object1 which has a list retrieved from another table.

请帮忙.

推荐答案

您无法按计划在一条语句中完成此操作.用不同的方式思考.有可能.

You can't do it in one statement as you plan to. Think in a different way. There are some possibilities.

  1. 您将创建对象1到表1以及对象2到表2的映射.在Object1的映射中,创建与Object2的一对多关系,并且该关系包含您选择的where条件"WHERE Object2.id>0".然后,您只需要HQL语句"from Object1".
    这是最好的和最休眠的方式,但是仅在where条件始终相同的情况下才有效.然后,您需要在table1和table2之间建立数据库关系(在您的示例中没有该数据库关系,但是可能存在,并且在您的示例中仅将其忘记了).

  1. You create the mapping for Object1 to the table1 and Object2 to table2. In the mapping of Object1 you create a one-to-many relation to Object2, and this relation contains the where condition "WHERE Object2.id>0" from your select. Then you only need the HQL statement "from Object1".
    This is the nicest and most hibernate-style possibility, but it only works if the where condition always is the same. And then you need a database relation between table1 and table2 (which you don't have in your example, but which probably exists and you only forgot it in your example).

    or

您只能先加载table1 SELECT new Object1(a.id) FROM Table1 a,然后循环遍历此列表的所有元素,然后加载第二个表FROM table2 b WHERE b.id>0并将其放入Object1类型的元素中.

You only load table1 first SELECT new Object1(a.id) FROM Table1 a and then in a loop over all elements of this list you load the second table FROM table2 b WHERE b.id>0 and put it into the element of type Object1.

or

您只先加载table1 SELECT new Object1(a.id) FROM Table1 a,并且在Object1中具有方法getObject2List(),该方法在第一次调用时从table2加载元素列表.

You only load table1 first SELECT new Object1(a.id) FROM Table1 a and in Object1 you have a method getObject2List() which at the first call loads the list of elements from table2.

这篇关于HQL新对象(..)中的新列表(..)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆