How to map a simple Hibernate join result to object with sub-objects?
I am confused about how to query for a list of objects with sub-objects in it.
I have two classes Execution and Order. And In Execution, I have the order object in it as a one-to-one relationship:
public class Execution {
private long id;
private Order order;
...
In the Execution.hbm.xml, I have configured Order as a many-to-one with unique="true".
<class name="Execution" table="executions">
<id name="executionId" type="long">
<generator class="native" />
</id>
<many-to-one name=开发者_高级运维"Order" unique="true" fetch="join" class="Order" />
</class>
Normally, we query for Execution objects with HibernateDAOSupport by using:
List<Execution> executions = getHibernateTemplate().find("from Execution");
With the above relationship, how do we query it and where can I find more information on persisting objects for complicated joins? I've tried:
List<Execution> list = getHibernateTemplate().find("from Execution e left outer join e.order");
But it's giving a ClassCastException as the query does not seem to return Execution objects with the extra order details in the Order object in Execution.
Is something wrong with the mapping? Or the query?
Thanks so much for any pointers!
-- update
Sorry for being a noob. Apparently the first query works. After reading tons of documentation on HQL, they weren't clear enough about this. Any better documentation would be helpful. Thanks...
This will not work, because you do not explicitly tell that you want only Execution entities in your result. It cannot figure it out, because of join to the order entity causes also Order attributes to be part of result.
List<Execution> list = getHibernateTemplate().find(
"from Execution e left outer join e.order");
Just add select and it will work fine:
List<Execution> list = getHibernateTemplate().find(
"select e from Execution e left outer join e.order");
But does it make sense to have a such a query is other question, in general you do not have to make joins in queries to have relationship attributes populated. Are they populated is controlled by setting fetching strategy to LAZY/EAGER or by depending about defaults.
In your case order should exist in query only if
- you want to limit results of query by some attribute in order (in such a case you can also avoid JOIN in your HQL query and just navigate through the path: e.order.id=2) or
relationship to order is lazy and you just want to ensure that order is fetched:
List list = getHibernateTemplate().find( "from Execution e left outer join fetch e.order");
@Mikko's and @JB's answer/comments are correct. To query the on the sub object just change the line
List<Execution> list = getHibernateTemplate().find(
"select e from Execution e left outer join e.order");
to
List<Execution> list = getHibernateTemplate().find(
"select e from Execution e left outer join e.order as o where o.id = ?");
精彩评论