query specified join fetching, but the owner of the fetched association was not present in the selec

来源:互联网 发布:java easyui使用 编辑:程序博客网 时间:2024/05/24 05:53

错误日志信息:

'deliveryAddressDao': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=c,role=null,tableName=customer,tableAlias=customer1_,origin=delivery_address deliveryad0_,columns={deliveryad0_.customer_id ,className=com.friendcom.dongzuo.entity.Customer}}] [select count(c.customerId) from com.friendcom.dongzuo.entity.DeliveryAddress addr left join fetch addr.customer c where c.customerId=?1]

原因分析:

     Hql 如下 :

 @Query("select count(addr.deliveryAddressId) from DeliveryAddress addr left join fetch  addr.customer c where c.customerId=?1")    long findByCustomerIdforTotalSize(int customerId);

  此处我希望加载DeliveryAddress下的customer,而我使用了fetch来立即抓取customer,错误就在这里.如果你使用fetch,那么fetch左边的连接对象(拥有者)一定要出现在select后,例如将上面改为select addr ... 这样就会执行正常,因为使用了fetch,Hibernate就会将需要fetch的对象(customer)立即加载在父对象(DeleveryAddress)中,而我的select确只是列出子对象,而拥有者(DelivieryAddress)并没有present(出席在结果集中),那么就会出现以上错误.


总结:

      如果使用了fetch,那么拥有者一定要present,也就是对象一定要被加载出来


0 0
原创粉丝点击