簡體   English   中英

如何在hibernate中對composite-id鍵屬性應用過濾器

[英]how to apply filter on composite-id key property in hibernate

我正在嘗試解決與hibernate中的過濾相關的問題,特別是在處理復合Id時

我有一個AttrDesc.hbm.xml

<hibernate-mapping>
    <class name="AttrDesc" table="ATTRDESC">
        <composite-id>
            <key-property name="attr_id" type="long"/>
            <key-property name="language_id"/>
        </composite-id>
        <property name="attrtype_id"/>
        <property name="name"/>
        <property name="description"/>
        <property name="description2"/>
        <property name="field1"/>
        <property name="groupname"/>
        <property name="qtyunit_id"/>
        <property name="noteinfo"/>
        <filter name="langFilter" condition=":langid=language_id"/>
    </class>
     <filter-def name="langFilter">  <filter-param name="langid" type="int"/>   </filter-def>
</hibernate-mapping>

問題:我無法在language_id上​​應用過濾器,而language_id是composite-id的一部分

在調試時我發現filter-param langid是一些如何將值設置為0,其中我使用下面的代碼行設置的實際值為-1

session.enableFilter("langFilter").setParameter("langid", -1);

注意:如果我將language_id移出復合ID,則該方法有效

任何幫助表示贊賞,謝謝

對於Hibernate jpa 2.1及以上版本,您應該稍微修改映射。

<composite-id name="attrDescId" class="AttrDescId">
    <key-property name="attr_id" type="long"/>
    <key-property name="language_id"/>
</composite-id>

因此,您需要更改模型類

public class AttrDesc {
    AttrDescId id;
    String name;
    //other fields
}
public class AttrDescId {
    long attr_id;
    String language_id;
}

然后使用root.get("attrDescId").get("language_id")您將能夠將過濾器放在language_id 精心,

CriteriaBuilder builder = sessionFactory.getCriteriaBuilder();
CriteriaQuery<AttrDesc> query = builder.createQuery(AttrDesc.class);
Root<AttrDesc> root = query.from(AttrDesc.class);
query.select(root).where(builder.equal(root.get("attrDescId").get("language_id"), "the filter value"));
Query<AttrDesc> q = sessionFactory.getCurrentSession().createQuery(query);
List<AttrDesc> attrDescList = q.getResultList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM