Hibernate Criteria API-访问联接的属性 [英] Hibernate Criteria API - accessing joined properties

查看:55
本文介绍了Hibernate Criteria API-访问联接的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常复杂的条件,我用它来检索,排序和分页服务器端数据.我摘录了以下摘录:

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();

陈述式(1)正常运行,陈述式(2)以下列例外结尾:

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

严重:Servlet REST的Servlet.service()抛出异常 org.hibernate.QueryException:无法解析属性: product.maturity日期:com.my.model.PriceRequest

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

我以"maturityDate"为例,但是除"product.id"外,所有产品属性均存在此问题.

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

我还尝试为产品(产品")添加别名.这样可以按到期日期("prod.maturityDate")进行排序,但是无法在子条件中添加限制.

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...

推荐答案

createCriteria(association)不会创建别名,但您需要一个别名

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

此外,您不能筛选关联并急于获取它,因为您可能会筛选出初始化它所需的关联实体,因此将忽略fetchmode.使用相关子查询

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();

这篇关于Hibernate Criteria API-访问联接的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆