org.hibernate.criterion.Order没有使用'。'解析属性。对于字符串

时间:2016-11-17 00:04:31

标签: java hibernate criteria hibernate-criteria

我尝试使用Hiberante条件API对结果进行升序或降序排序。

我定义了A类; (简化格式)

@Entity
public class A {

    @ManyToOne
    @JoinColumn(name = "yy")
    B b;

    ...
}

我的B实体就像;

@Entity
public class B  extends C {
    ...
}

@Entity
public class C  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", updatable = false, nullable = false)   
    private Long id;

    @NotNull
    @NotEmpty
    @Size(max = 64)
    @Pattern(regexp = REGEX_CODE, message = "Must be valid LOB  Code")
    @Column(name = "code", updatable = false, nullable = false) 
    private String code;


    }
}

当我使用时;

criteria.addOrder(Order.asc(b.id));  //works

 criteria.addOrder(Order.asc(b.code)); //Throws error

Error


Caused by: org.hibernate.QueryException: could not resolve property: manufacturer.code of: xx.core.model.wetstock.ATG
    at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:62)
    at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:77)
    at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:43)
    at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1793)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:510)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:469)
    at org.hibernate.criterion.Order.toSqlString(Order.java:109)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getOrderBy(CriteriaQueryTranslator.java:414)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:106)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:75)
    at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:80)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1841)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:365)
    at com.cdi.crud.infra.Crud.list(Crud.java:450)
    at com.cdi.crud.infra.CrudService.paginate(CrudService.java:210)

当我看到B表时,我看到列“id”就在那里。没有提供其他专栏。 其他列在表“C”下创建。

所以,如果我使用“。”要使用表C中列出的列进行排序,我得到的属性无法解决问题。

我怎样才能克服这个?

1 个答案:

答案 0 :(得分:1)

我通过添加“别名”来修复。

eg: criteria.createAlias("b" ,"bob").addOrder(Order.asc(bob.code))