Hibernate HQL constructor expression on collection object

时间:2018-04-20 21:23:44

标签: hibernate jpa constructor expression hql

I have following

Order {
  Date date, 
  String title, 
  List<Item> items
}

Item {
  String name, 
  String qty, 
  ... [other non-related information]
}

I can write an HQL which will return me a list of Orders with items in it.

SELECT order 
FROM Orders order 
  LEFT JOIN FETCH oder.items 
WHERE item.qty > 2

My question is, can I put Constructor Expression on "Item" to get only (name & qty)?

1 个答案:

答案 0 :(得分:0)

不。但是,您可以使用entity graph仅加载您感兴趣的Item个字段。

请注意:

  • 您需要确保启用实体增强功能(否则基本属性仍会被急切加载)
  • 您的应用可能无法访问Itemidname以外的任何qty属性,否则hibernate可能会尝试初始化其余的实体属性(将发出数据库查询或将抛出LazyInitializationException,具体取决于实体状态)。这包括您的应用程序的“不太明显”部分,如JSON序列化库