Hibernate按内部bean的属性排序?

在我的域模型中,我有以下类.一个’UserProfile’有一个’SecurityPrincipal’

class SecurityPrincipal{
 private String loginId;
 private String password;
 private Date registeredData;
 private int status;
}



class UserProfile {

private String name;
private String company;
private SecurityPrincipa principal

}

我想获得’UserProfile’对象的排序结果,它对于简单的属性也可以正常工作.

DetachedCriteria criteria=DetachedCriteria.forClass(UserProfile.class);     

criteria.addOrder(Order.asc("name");

但是当我尝试访问内部bean(SecurityPrincipal实例)的属性时

criteria.addOrder(Order.asc("principal.status");

Hibernate给出错误:

Caused by:
org.hibernate.QueryException: could
not resolve property:
securityPrincipal.status of:
com.bigg.ibmd.usermanagement.model.UserProfile
at
org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
at
org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:59)
at
org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)

如何按属性对结果进行排序?

谢谢

解决方法:

尝试这个:

DetachedCriteria criteria=DetachedCriteria.forClass(UserProfile.class);         
criteria.createAlias("principal", "p");
criteria.addOrder(Order.asc("p.name"));

我没有尝试过,也不确定这是最好的方法,但是我认为它应该可以工作.

上一篇:mybatis逆向工程介绍


下一篇:Restrictions----用法