【问题标题】:How to query composite element of collection? (cannot create element join for a collection of non-entities)如何查询集合的复合元素? (不能为非实体集合创建元素连接)
【发布时间】:2012-09-20 08:27:58
【问题描述】:

我有一门课

    <class name="Goods">
        ...
    <map name="names" lazy="false" fetch="join">
        <key not-null="true" />
        <map-key column="LANGUAGE_CODE" type="language" length="2"/>
        <composite-element class="Goods$Names">
            <property name="name" not-null="true" type="text"/>
            <property name="description" type="text"/>
        </composite-element>
    </map>

    ...

</class>

当我尝试像这样按名称搜索商品时出现问题:

session.createQuery("select g from Goods g where g.names[:lang].name = 'Some goods name'")


javax.servlet.ServletException: java.lang.IllegalArgumentException: Cannot create element join for a collection of non-entities!

Hibernate 真的不可能做到这一点吗?
我真的需要将商品名称设为具有自己 ID 的实体吗? 有什么解决办法吗?

【问题讨论】:

  • 为什么不把它做成实体呢?
  • 我不想要实体,因为我不想要额外的 id 列。所以我的第一个方法是创建具有复合 id 的实体。但最后我明白了我必须如何创建我的查询。所以这个查询很简单: select g from Goods g join g.names names with index(names) = :lang where names.name='some goods names'

标签: java hibernate collections


【解决方案1】:

如果你想用复合键创建实体类,那么你必须在 java 中使用内部类,如果你在休眠状态下制作实体,那么你必须维护关系(比如实体之间的多对多、一对多、一对一、多对一),然后 Hibernate 具有在内部执行连接的功能,您不需要自己执行,这是我这边的全部提示,请尝试一下,即使这会让您的设计很好地被其他人理解。

【讨论】:

  • 如果这个答案解决了您的问题,请批准它,以便对其他人也有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-22
  • 2012-10-31
  • 2019-11-05
  • 1970-01-01
相关资源
最近更新 更多