views:

226

answers:

1

In my domain model I have following Classes.A 'UserProfile' has one '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

}

I want to get the sorted results of 'UserProfile' objects and it works fine for simple properties.Like

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

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

But when I try to access properties of inner bean (SecurityPrincipal instance) like

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

Hibernate gives the error:

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)

How can I sort my results by properties-of-a-property?

Thanks

+3  A: 

Try this:

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

I haven't tried it, nor am I sure that it's the nicest way, but I think it should work.

skaffman
thanks it worked!
umanga