Hibernate Criteria API - accessing joined properties

谁说胖子不能爱 提交于 2019-12-24 00:34:35

问题


I have a pretty complex criteria, which I use to retrieve, sort and page server-side data. I stripped down the following excerpt:

    // create criteria over a bunch of tables...
    Criteria testCriteria = getSession().createCriteria(PriceRequest.class)
            .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
            .setFetchMode("pricedBy", FetchMode.JOIN)
            .setFetchMode("canceledBy", FetchMode.JOIN)
            .setFetchMode("product", FetchMode.JOIN)
            .setFetchMode("product.underlyings", FetchMode.JOIN)
            .setFetchMode("product.tradedBy", FetchMode.JOIN)
            .setFetchMode("product.requestedBy", FetchMode.JOIN)
            .setFetchMode("fileUploads", FetchMode.JOIN);

    // add various filter fields (only if required in real code)...
        Criteria subCriteria = testCriteria
                .createCriteria("product", JoinFragment.LEFT_OUTER_JOIN)
                .setFetchMode("underlyings", FetchMode.JOIN)
                .add(Restrictions.ge("maturityDate", new Date()));

        testCriteria.addOrder(Order.desc("product.id")); // (1)
        testCriteria.addOrder(Order.desc("product.maturityDate")); // (2)

        List list = testCriteria.list();

Statement (1) runs fine, statement (2) ends with the following exception:

SEVERE: Servlet.service() for servlet rest threw exception org.hibernate.QueryException: could not resolve property: product.maturityDate of: com.my.model.PriceRequest

I picked "maturityDate" as an example, but I'm having this issues with all product properties except "product.id".

I also tried adding an alias for product ("prod"). This allows to sort by maturity date ("prod.maturityDate"), but fails on adding the restriction to subcriteria.

I so much have no clue what's going wrong...


回答1:


createCriteria(association) does not create an alias but you need one for the orderby

Also you cant filter on an association and eager fetch it because you might filter out entities of the association which are needed to initialize it hence the fetchmode is ignored. Use a correlated subquery

// create criteria over a bunch of tables...
Criteria testCriteria = getSession().createCriteria(PriceRequest.class)
        .setFetchMode("pricedBy", FetchMode.JOIN)
        .setFetchMode("canceledBy", FetchMode.JOIN)
        .createAlias("product", "prod")
        .setFetchMode("prod.underlyings", FetchMode.JOIN)
        .setFetchMode("prod.tradedBy", FetchMode.JOIN)
        .setFetchMode("prod.requestedBy", FetchMode.JOIN)
        .setFetchMode("fileUploads", FetchMode.JOIN)
        .addOrder(Order.desc("prod.maturityDate"));       // (2)

// add various filter fields (only if required in real code)...
if (...)
{
    Criteria subCriteria = getSession().createCriteria(PriceRequest.class)
        .createCriteria("product", JoinFragment.LEFT_OUTER_JOIN)
        .add(Restrictions.ge("maturityDate", new Date()));
        .setResultTransformer(Projection.id)

    testCriteria = testCriteria
        .add(Subqueries.PropertyIn("id", subCriteria));
        .setFetchMode("underlyings", FetchMode.JOIN)
}

List list = testCriteria.list();


来源:https://stackoverflow.com/questions/9872191/hibernate-criteria-api-accessing-joined-properties

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