Using Hibernate 3.0.2, I am trying to execute criteria queries using property projections that traverse the object graph something like this:
Code:
Criteria criteria = session.createCriteria(Root.class);
criteria.setProjection(Projections.property("propA.propB.propC"));
criteria.list();
where propA is a property of Root, propB is a property of propA, and propC is a property of propB.
It appears that Hibernate can handle projections out through "propA" without an alias, but I cannot get it to work for nested properties in an object graph. If your property path has several steps in it, this can mean a lot of alias creation.
It seems that the API relies on the use of aliases to make this kind of projection work, but that seems to me to be a lot of work to accomplish
something that seems pretty straight forward.
I have tried to create implementations of PropertyProjection that creates the alias tree itself, but then you need to make sure that the aliases are unique, otherwise you will get more errors. I do not have enough insight into the internals of Hibernate to know the best path to follow to make this solution work.
Ultimately, I would like to be able to create PropertyProjections using the full property path from the criteria root class, like you might do in HQL. This makes it easier to create these queries dynamically, which is part of the requirement for my current project.
So, I need to know if I am missing something about how to do this easily, if it cannot be done without creating a lot of aliases, or if Hibernate can be updated to make this work.
Thanks.
Hibernate version:3.0.2
Mapping documents:N/A
Code between sessionFactory.openSession() and session.close():
Criteria criteria = session.createCriteria(Root.class);
criteria.setProjection(Projections.property("propA.propB.propC"));
criteria.list();
Full stack trace of any exception that occurs:
org.hibernate.QueryException: could not resolve property: partA.partB.partC of: x.x.x.Root
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.BasicEntityPersister.toType(BasicEntityPersister.java:1094)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getType(CriteriaQueryTranslator.java:442)
at org.hibernate.criterion.PropertyProjection.getTypes(PropertyProjection.java:36)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:297)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:81)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1236)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:299)
Name and version of the database you are using:N/A
The generated SQL (show_sql=true):N/A
Debug level Hibernate log excerpt:N/A