`

org.hibernate.QueryException: illegal attempt to dereference collection

 
阅读更多

我写的hql为:

from Department as d where d.employees.name='Tom';

运行时出现异常:org.hibernate.QueryException: illegal attempt to dereference collection

是因为:在上面的HQL语句中,Department的关联实体employees是一个集合,而不直接是一个Employee实体。

在Hibernate3.2.2以前的版本,Hibernate会对关联实体自动使用隐式的inner join,

也就是说如下SQL语句不会有任何问题 :from Department as d where d.employees.name='Tom';

从Hibernate3.2.3以后,Hibernate改变了这种隐式的inner join的策略

对于如下这条语句:

from Department as d where d.employees.name='Tom';

如果employees是普通组件属性,或单个的关联实体,则Hibernate会自动生成隐式的inner join

如果myEvents是也一个集合,那么对不起!系统将会出现 org.hibernate.QueryException: illegal attempt to dereference collection异常。
据Hibernate官方说法:
这样可以让这使得隐含关联更具确定性(原文:This makes implicit joins more deterministic )。

推荐这样写:

from Department as d inner join fetch d.employees e where e.name='Tom';

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics