java 休眠异常:非法尝试取消引用集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6327122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate Exception : Illegal attempt to dereference a collection
提问by Sripaul
Consider the following Entities.
考虑以下实体。
public class Product{
int id;
Date effectiveDate;
Date expiryDate;
Set<Inventory> productInventories;
}
public class Inventory{
int invId;
Date inventoryDate;
boolean soldOut;
int availableQuantity;
Product product;
}
The above two entities maps to tables Product and Inventory respectively.
以上两个实体分别映射到表 Product 和 Inventory 。
Now I have to retrieve Products based on certain conditions in Product entity as well as Inventory entity.
现在我必须根据产品实体和库存实体中的某些条件检索产品。
For ex the conditions are given travel start date and travel end date has to suit effective and expiry date of Product. Product Inventory should have availableQuantity > 0.
例如,条件是旅行开始日期和旅行结束日期必须适合产品的生效日期和到期日期。产品库存应该有可用数量 > 0。
To do this how can i write the hql. Can i write something like the following
要做到这一点,我该如何编写 hql。我可以写如下内容吗
Query query = session.createQuery("from Product As product " +
"where product.effectiveDate <= :travelStartDate "+
"AND product.expiryDate >= :travelEndDate " +
"AND product.productInventories.availableQuantity >0 ");
When i execute the above query, it throws a Illegal attempt to dereference a collection exception.
当我执行上述查询时,它会抛出一个非法尝试取消引用集合异常。
回答by Alex Gitelman
You probably want something like
你可能想要类似的东西
from Product as product
inner join product.productInventories inv with inv.availableQuantity>0
See http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-joins
见http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-joins