Hibernate criteria problem with composite key

2019-04-10 02:49发布

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条回答
Fickle 薄情
2楼-- · 2019-04-10 03:12

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);
查看更多
登录 后发表回答