Hibernate criteria problem with composite key

£可爱£侵袭症+ 提交于 2019-12-06 04:49:34

问题


I got this hibernate mapping:

<class name="CoverageTerm" table="coverage_term">

    <composite-id name="id" class="CoverageTermPK">
        <key-many-to-one name="productTerm" class="ProductTerm">
            <column name="termtype_id"></column>
            <column name="product_id" ></column>
        </key-many-to-one>
        <key-many-to-one name="productCoverage" class="ProductCoverage" column="product_coverage_id"></key-many-to-one>
    </composite-id>

    <property name="data"/>
</class>

This is a simple composite key mapping with a relation to table productCoverage and a composite key relation to productterm.

Now the problem comes in my search function:

public CoverageTerm getCoverageTermFromProductTermCoverage(ProductTerm productTerm, ProductCoverage productCoverage) {
    Criteria critCt = getSession().createCriteria(CoverageTerm.class);
    Criteria critCtId = critCt.createCriteria("id");
    critCtId.add(Restrictions.eq("productTerm", productTerm));
    critCtId.add(Restrictions.eq("productCoverage", productCoverage));
    return (CoverageTerm) critCt.uniqueResult();
}

This should let me make a subcriteria on "id" (which is the primary key, CoverageTermPK) and add restrictions on it, but when I run it I get the error message:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: productTerm of: CoverageTerm

This feels strange, shouldn't it get the CoverageTermPK there? If I try with a subcriteria on the "data" property the criterias work, I just don't seem to be able to get the PK on the "id" subcriteria.

Any ideas as to why this is happening?


回答1:


Not sure about your specific class structure but try to add id this way instead of separate criteria:

Restrictions.eq("id.productTerm", productTerm);
Restrictions.eq("id.productCoverage", productCoverage);


来源:https://stackoverflow.com/questions/6120852/hibernate-criteria-problem-with-composite-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!