Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate criteria problem with composite key

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?

like image 339
Dytut Avatar asked Dec 16 '22 13:12

Dytut


1 Answers

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);
like image 157
Alex Gitelman Avatar answered Jan 07 '23 13:01

Alex Gitelman