Hibernate:如何从多个类的查询中获取结果

发布于 2024-12-12 07:18:34 字数 344 浏览 0 评论 0原文

如果我的查询包含一个类,例如:

query = session.createQuery("select u from User as u");
queryResult = query.list();

然后我迭代它,其中 queryResultUser 类的对象。

那么如何从包含多个类的查询中获取结果呢?例如:

select u, g from User as u, Group as g where u.groupId = g.groupId and g.groupId = 1

If my query contains one class, like:

query = session.createQuery("select u from User as u");
queryResult = query.list();

then I iterate it, where queryResult is an object of User class.

So how to get result from query which contains more than one class? For example:

select u, g from User as u, Group as g where u.groupId = g.groupId and g.groupId = 1

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

比忠 2024-12-19 07:18:34
for (Object[] result : query.list()) {
    User user = (User) result[0];
    Group group = (Group) result[1];
}
for (Object[] result : query.list()) {
    User user = (User) result[0];
    Group group = (Group) result[1];
}
风吹雨成花 2024-12-19 07:18:34

我相信你可以使用元组来做到这一点,但更重要的是,如果你的组和用户是相关的,就像该查询似乎建议用户应该有一个组字段(不要在你的用户类中使用groupId,hibernate应该为你解决这个问题)。如果是这种情况,您可以简单地使用 select u from User u join fetch u.group g where g.groupId = :id 进行查询(然后使用 query.setParameter(1, 该查询中的 fetch 关键字使其成为急切加载,因此两个对象都将返回到 hibernate ,

hibernate 将使用 user.id 访问 Group 对象。 getGroup()。

You can do that using Tuples I believe, but more importantly, if your Group and User is related like that query seems to suggest User should have a Group field (don't use groupId in your User class, hibernate should sort this out for you). If that's the case you can simply query it using select u from User u join fetch u.group g where g.groupId = :id (then set the id using query.setParameter(1, id);.

The fetch keyword in that query makes it an eager load so both objects will be returned to hibernate which will return the User object to you. Access the Group object using user.getGroup().

耶耶耶 2024-12-19 07:18:34

当您选择单个实体时,query.list() 将返回包含您的实体的 ObjectList

当您选择多个实体时,query.list() 将返回 Object[]List。数组的每个元素代表一个单独的实体。

在这里阅读更多内容: http:// docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-select

When you select a single entity, query.list() will return a List of Object containing your entities.

When you select multiple entities, query.list() will return a List of Object[]. Each element of the array represents a separate entity.

Read more here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-select

黯然#的苍凉 2024-12-19 07:18:34

您还可以创建一个构造函数并返回一个对象:

假设类 Family 有一个适当的构造函数 - 作为实际类型安全的 Java 对象:

select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr

或一个列表:

select new list(mother, offspr, mate.name)
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-select

Also you can create a constructor and return a object:

Assuming that the class Family has an appropriate constructor - as an actual typesafe Java object:

select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr

Or a list:

select new list(mother, offspr, mate.name)
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-select

孤独岁月 2024-12-19 07:18:34

如果将 query.list() 转换为 Object[] 不起作用。您可以先将其转换为对象,然后将该对象转换为 Object[],如下所示

for (Object r: query.list()) {
        Object[] r1 = (Object[]) r;
        User user = (User) result[0];
        Group group = (Group) result[1];
     }

If casting the query.list() to Object[] does not work. You can first cast it to an Object and then cast that Object to Object[] like this

for (Object r: query.list()) {
        Object[] r1 = (Object[]) r;
        User user = (User) result[0];
        Group group = (Group) result[1];
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文