org.hibernate.QueryException:无法直接在组件上创建Criteria对象

时间:2016-11-22 04:32:17

标签: hibernate criteria hibernate-criteria criteria-api

在尝试基于嵌入字段进行排序时,我遇到了上述问题;

例如:我尝试使用属性tObservation.raw.waterLevel.metre进行排序。 但得到以下例外。

Caused by: org.hibernate.QueryException: Criteria objects cannot be created directly on components.  Create a criteria on owning entity and use a dotted property to access component property: tObservation.raw
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathInfo(CriteriaQueryTranslator.java:251)

我创建了像;

这样的别名
criteria.createAlias("tObservation", "0").createAlias("0.raw","1").createAlias("1.waterLevel","2").addOrder(Order.asc("2.meter"))

为什么我得到那个例外?

以上代码适用于两个级别 例如:tObservation.id

p.s:如果我尝试;(没有别名)

criteria.addOrder(Order.asc("tObservation.raw.waterLevel.metre"))

我得到同样的例外。

我的班级结构

@Entity
class tank {

Observation tObservation;

}


@Entity
class Observation {

@Embedded
RawObservation raw;

}

@Embeddable
class RawObservation{

    @Embedded
Length waterLevel;
}

@Embeddable
class Length{
BigDecimal metre

}

2 个答案:

答案 0 :(得分:0)

使用类似的东西

Criteria cObservation = criteria.createCriteria("tObservation");
Criteria cRaw = cObservation.createCriteria("raw");
Criteria cWaterLevel = cRaw.createCriteria("waterLevel");
cWaterLevel.addOrder(Order.asc("meter"))

答案 1 :(得分:0)

你也可以尝试这样:

Criteria cObserv = criteria.createCriteria("tObservation");
cObserv.createAlias("raw", "r");
cObserv.createAlias("r.waterLevel", "rw");

cObserv.createAlias("rw.metre", "rwm");
or
cObserv.setFetchMode("rw.metre", FetchMode.JOIN);

cObserv.addOrder(Order.asc("rw.metre"))