【问题标题】:"org.hibernate.QueryException: could not resolve property" while sorting by non-PK column“org.hibernate.QueryException:无法解析属性”,同时按非 PK 列排序
【发布时间】:2015-04-10 19:54:30
【问题描述】:
Class A {
 private long id; //PK
 private B b; // FK many-to-one

 public long getId() {
    return this.id;
 }

 public void setId(long id) {
    this.id = id;
 }

 public B getB() {
    return this.b;
 }

 public void setB(B b) {
    this.b = b;
 }
}

Class B {
 private String code; // PK

 private String name;

 public String getCode() {
    return this.code;
 }

 public void setCode(String code) {
    this.code = code;
 }

 public String getName() {
    return this.name;
 }

 public void setName(String name) {
    this.name = name;
 }   
}

检索 A 对象列表并按 b.code 或 b.name 排序,如下所示

public List<A> loadListOfAs(final Integer id) {
        final DetachedCriteria criteria = DetachedCriteria.forClass(A.class);
        criteria.add(Restrictions.eq("id", id));
        //criteria.addOrder(Order.asc("b.code")); -- uncommenting this line and deleting the line below works
        criteria.addOrder(Order.asc("b.name")); // Using this line errors out         
        final List<A> returnList = findByCriteria(criteria); 
        return returnList;
    }   

按 b.code 排序有效,但按 b.name 排序会引发以下问题 org.hibernate.QueryException:无法解析属性:b.name of:A

【问题讨论】:

    标签: hibernate api sorting criteria


    【解决方案1】:

    所以这是实现这一目标的标准 -

    public List<A> loadListOfAs(final Integer id) {
    final DetachedCriteria c1= DetachedCriteria.forClass(A.class);
    c1.add(Restrictions.eq("id", id));
    DetachedCriteria c2 = c1.createCriteria("b");
    c2.addOrder(Order.asc("name")); 
    
    final List<A> returnList = findByCriteria(c1); 
    return returnList;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      相关资源
      最近更新 更多