Hi,
maybe I'm missing something, but I cannot sort out the problem.
I've three entities (Supplier, Province, Nation):
What follows is just an example of what I would need to do, but it gives the malfunctioning.
Suppose I want the list of the suppliers ordered by the nation code, which I can reach passing through the province (property addressProvince). My province has a property nation of Nation. Nation has code as key and description as property.
This is a simple schema of my relationships:
(Supplier) -------- addressProvince ----> (Province) -------- nation ----> (Nation)
This would be the code to get "list of the suppliers ordered by the nation code"
Code:
Criteria crit = sess.createCriteria(Supplier.class);
Criteria critAddressProvince = crit.createCriteria("addressProvince","addressProvince");
Criteria critAddressNation = critAddressProvince.createCriteria("nation","nation");
Order order = Order.asc("addressProvince.nation.code");
crit.addOrder(order);
this works fine.
Now: suppose I would like to get "list of the suppliers ordered by the nation description"
Then, this would be my code:
Code:
Criteria crit = sess.createCriteria(Supplier.class);
Criteria critAddressProvince = crit.createCriteria("addressProvince","addressProvince");
Criteria critAddressNation = critAddressProvince.createCriteria("nation","nation");
Order order = Order.asc("addressProvince.nation.description");
crit.addOrder(order);
This is giving me the following error:
Code:
org.hibernate.QueryException: could not resolve property: nation.description of: com.logit.hibernate.Province
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:46)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:61)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1346)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:434)
I tried to use the
criteria.setFetchMode(association, FetchMode.JOIN);
but with no success.
I tried to use the
Code:
Criteria crit = sess.createCriteria(Supplier.class);
Criteria critAddressProvince = crit.createCriteria("addressProvince","addressProvince");
Criteria critAddressNation = critAddressProvince.createCriteria("nation","nation");
Order order = Order.asc("description");
critAddressNation.addOrder(order);
This works, but is not what I need. As I said my problem is more generic, this example is just a simplificatin of a more generalized issue.
This is not enough for me because in case order by on multiple properties, it would be impossible to define the order.
Suppose I want to order by nation.description and province.description
This
Code:
Criteria crit = sess.createCriteria(Supplier.class);
Criteria critAddressProvince = crit.createCriteria("addressProvince","addressProvince");
Criteria critAddressNation = critAddressProvince.createCriteria("nation","nation");
critAddressNation.addOrder(Order.asc("description"));
critAddressProvince.addOrder(Order.asc("description"));
is (obviously) the same of:
Code:
Criteria crit = sess.createCriteria(Supplier.class);
Criteria critAddressProvince = crit.createCriteria("addressProvince","addressProvince");
Criteria critAddressNation = critAddressProvince.createCriteria("nation","nation");
critAddressProvince.addOrder(Order.asc("description"));
critAddressNation.addOrder(Order.asc("description"));
.
While
Code:
Criteria crit = sess.createCriteria(Supplier.class);
Criteria critAddressProvince = crit.createCriteria("addressProvince","addressProvince");
Criteria critAddressNation = critAddressProvince.createCriteria("nation","nation");
crit.addOrder(Order.asc("addressProvince.description"));
crit.addOrder(Order.asc("addressProvince.nation.description"));
is different (the order depends on the add sequence)
Code:
Criteria crit = sess.createCriteria(Supplier.class);
Criteria critAddressProvince = crit.createCriteria("addressProvince","addressProvince");
Criteria critAddressNation = critAddressProvince.createCriteria("nation","nation");
crit.addOrder(Order.asc("addressProvince.nation.description"));
crit.addOrder(Order.asc("addressProvince.description"));
Any help would be appreciated.
Thanks
Marco