集合上的Hibernate查询过滤器 [英] Hibernate query filter on collection

查看:74
本文介绍了集合上的Hibernate查询过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行以下查询:

from Item i where i.categoryItems.catalogId = :catId

但是这产生了以下异常:非法尝试取消引用集合 因此,我用Google搜索了一下,发现了这个Hibernate论坛帖子 https://forum.hibernate.org/viewtopic. php?p = 2349920 ,建议我执行以下操作:

That however yields in following exception: illegal attempt to dereference collection So I googled, found this Hibernate forum post https://forum.hibernate.org/viewtopic.php?p=2349920 that recommended me to do the following:

from Item i, IN (i.categoryItems) WHERE i.catalogId = :catId

这种作品,但是有一个问题: 它返回一个带有Item对象和CategoryItem对象的Object数组.我只对单个Item对象(列表)感兴趣

This kind of works, but there's a problem with this: It returns me an Object array with Item object and CategoryItem object. I'm only interested in the single Item object (List)

我对商品"的映射:

My mapping of 'Item':

<hibernate-mapping package="be.xx.xx.xx.xx.domain" default-access="field">
  <class name="Item" table="ITEM">  
    <id name="articleId" column="article_id" type="long">
        <generator class="assigned" />
    </id>
...
...
        <set name="categoryItems" table="CATEGORY_ITEM">
            <key column="item_id" />
            <one-to-many class="be.xx.xx.xx.xx.domain.CategoryItem" />
    </set>
</class>
</hibernate-mapping>

有人有什么主意吗?

谢谢

推荐答案

尝试:

SELECT i FROM Item i inner join i.categoryItems cat WHERE cat.id = :catID

说明: 您尝试的导航:i.categoryItems.catalogId仅适用于1:1或n:1关系,而不适用于1:n. -对于1:n,您必须使用显式联接操作.

Explanation: The navigation you have tried: i.categoryItems.catalogId works only for 1:1 or n:1 relations, but not for 1:n. -- For 1:n you have to use the explicite join operation.

这篇关于集合上的Hibernate查询过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆