-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate 'join' fetching weird behaviour
PostPosted: Wed Mar 14, 2012 2:50 am 
Newbie

Joined: Wed Mar 14, 2012 2:45 am
Posts: 1
I have a user and a message table. User to Message are one-to-many relationships and Message to User are many-to-one relationships. I've marked one of the many-to-one as fetch join. When I 'get' a single Message, Hibernate runs a join query, but when I fetch all the messages, Hibernate runs select queries instead of a join. What could be the reason? Following are the details:


Relationship between them:

User

<set name="messagesForFromUserUid" lazy="true" table="message" inverse="true" cascade="save-update">
<key>
<column name="from_user_uid" not-null="true" />
</key>
<one-to-many class="repository.Message" />
</set>
<set name="messagesForToUserUid" lazy="true" table="message" fetch="select">
<key>
<column name="to_user_uid" not-null="true" />
</key>
<one-to-many class="repository.Message" />
</set>

Message

<many-to-one name="userByFromUserUid" class="repository.User" fetch="join" lazy="false">
<column name="from_user_uid" not-null="true" />
</many-to-one>
<many-to-one name="userByToUserUid" class="repository.User" fetch="select" lazy="proxy">
<column name="to_user_uid" not-null="true" />
</many-to-one>

When I fetch a single message object, Hibernate runs one join query as expected:

Message m = (Message) s.get(Message.class, 2);

Hibernate:
select
message0_.message_uid as message1_1_1_,
message0_.from_user_uid as from2_1_1_,
message0_.to_user_uid as to3_1_1_,
message0_.message_text as message4_1_1_,
message0_.created_dt as created5_1_1_,
user1_.user_uid as user1_0_0_,
user1_.user_name as user2_0_0_,
user1_.user_password as user3_0_0_,
user1_.email as email0_0_,
user1_.first_name as first5_0_0_,
user1_.last_name as last6_0_0_,
user1_.created_dt as created7_0_0_
from
hello.message message0_
inner join
hello.user user1_
on message0_.from_user_uid=user1_.user_uid
where
message0_.message_uid=?

But when I fetch all the messages in one go, Hibernate runs select queries instead:

List<Message> l = s.createQuery("from Message").list();

Hibernate:
select
message0_.message_uid as message1_1_,
message0_.from_user_uid as from2_1_,
message0_.to_user_uid as to3_1_,
message0_.message_text as message4_1_,
message0_.created_dt as created5_1_
from
hello.message message0_
Hibernate:
select
user0_.user_uid as user1_0_0_,
user0_.user_name as user2_0_0_,
user0_.user_password as user3_0_0_,
user0_.email as email0_0_,
user0_.first_name as first5_0_0_,
user0_.last_name as last6_0_0_,
user0_.created_dt as created7_0_0_
from
hello.user user0_
where
user0_.user_uid=?

Hibernate:
select
user0_.user_uid as user1_0_0_,
user0_.user_name as user2_0_0_,
user0_.user_password as user3_0_0_,
user0_.email as email0_0_,
user0_.first_name as first5_0_0_,
user0_.last_name as last6_0_0_,
user0_.created_dt as created7_0_0_
from
hello.user user0_
where
user0_.user_uid=?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.