Let's say I have the following three entities:
class Thing {
User owner;
}
class UserFriend {
User user;
User friend;
}
class User {
// collection key is friend in UserFriend
Set<UserFriend> friendUsers;
}
where they of course have mapping information, getters/setters, ids and all that stuff.
Here is the query I need to construct:
Find all Things belonging to my friends
The current implementation is (where ME is a previously retrieved User):
Criteria criteria = session.createCriteria( Thing.class )
.createCriteria( "owner" )
.createCriteria( "friendUsers" )
.add( Restrictions.eq( "user", ME );
This is the pseudo-statement being generated:
select thing from Things thing inner join Users user on thing.owner = user inner join UserFriends userfriend on user = userfriend.friend where userfriend.user = ME
What I need to do is avoid the unnecessary join on Users, so what I'm looking for is:
select thing from Things thing inner join UserFriends userfriend on thing.owner = userfriend.friend where userfriend.user = ME
I've tried all sorts of different rewrites of this query and can't manage to get rid of that join. Shouldn't Hibernate optimize out that unused inner join, or am I missing something? I'd appreciate any advise you might have.
|