Reputation: 747
A User
is 1-to-1 with Car
. But User
not always has a Car
.
query = session.createQuery("select u from User as u join fetch u.car");
I get only users with cars. How do i modify this query to get all users?
I'm using fetch because i need user.car outside of the session, am i doing this right?
Thanks.
Upvotes: 3
Views: 2270
Reputation: 8160
Do you need the car in the result? Otherwise a "select u from User u" should do the job.
If car can be null but you need it to be retrieved you can do a join fetch "select u from User u left outer join fetch u.car". Hope this is correct syntax. The outer join makes sure you get results even with null relations. In this case a join fetch should be enough.
there are some more (and working) examples ("join fetch") in the hibernate docs: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/
Upvotes: 1
Reputation: 10151
If you just want to get all users, then why not do:
List users = session.createCriteria(User.class).list();
if your User class maps to Cars, then the User objects that are returned will have the Car data associated with it, Hibernate will return this data automatically (depending on the mode, it might be lazy loaded, i.e. it will only retrieve the actual Cat data once you try to use it).
Upvotes: 2