解决org.hibernate.QueryException illegal attempt to dereference collection 异常错误

在项目中,会遇到一对多的实体关系映射,而恰恰又要查出关联的实体属性,在HQL中使用join语句关联两个实体对象,如下代码:

1 select new CustomerRequirement(c.pName,c.salesMan,c.pDate) from CustomerRequirement c inner join c.requirementDeatils r on c = c.requirementDeatils.customerRequirement

运行发现报如下错误:

org.hibernate.QueryException illegal attempt to dereference collection

 

这是因为在上面的HQL语句中,CustomerRequirement的关联实体requirementDeatils是一个Set集合,而不是一个实体。在 Hibernate3.2.2以前的版本,Hibernate会对关联实体自动使用隐式的inner join,也就是说使用上面的HQL语句是毫无问题的。

但是在Hibernate3.2.2版本以后,Hibernate改变了这种策略。它使用如下策略来关联实体。

 

同样对于上面的HQL语句。如果requirementDeatils是一个单个的关联实体或者是一个普通的属性,那么hibernate就会自动使用隐式的inner join。但是如果requirementDeatils是一个集合,那么就会出现org.hibernate.QueryException illegal attempt to dereference collection异常,对于解决方案就是,要么退回hibernate3.2.2版本以前,要么使用如下形式的HQL语句:

 

select new CustomerRequirement(c.pName,c.salesMan,c.pDate) from CustomerRequirement c inner join fetch c.requirementDeatils r on c = c.requirementDeatils.customerRequirement

 

posted on 2016-05-10 15:06  ++i和i++ 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/ao-xiang/p/5477902.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值