HQL和一对多查询 [英] HQL and one-to-many queries

查看:274
本文介绍了HQL和一对多查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Player {
List< Item>库存;
}

class物品{
列表< Enchantment>附魔;
}

class Enchantment {
boolean isSuperiorEnchantment;
}

我需要构建一个HQL查询,返回给我一个所有玩家列表至少有一件物品具有附魔,其中 isSuperiorEnchantment 标志设置。我不能在我的生活中想出一种方法来在HQL中表达这一点。



有什么想法?

解决方案

假设上面所有的映射都是合适的,那么您正在查找的查询是:

 从Player中选择p 
作为p
left加入p.inventory作为i $ b $ left加入i.enchantments作为e
其中e.isSuperiorEnchantment = 1


I have Hibernate domain objects that looks like this:

   class Player {
      List<Item> inventory;
   }

   class Item {
      List<Enchantment> enchantments;
   }

   class Enchantment {
      boolean isSuperiorEnchantment;
   }

I need to construct an HQL query that returns to me a list of all players that have at least one item with an enchantment on it that has the isSuperiorEnchantment flag set. I can't for the life of me figure out a way to express this in HQL.

Any ideas?

解决方案

Assuming the appropriate mappings on all of the above, the query you're looking for is:

select p
from Player as p
  left join p.inventory as i
  left join i.enchantments as e
where e.isSuperiorEnchantment = 1

这篇关于HQL和一对多查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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