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.  [ 3 posts ] 
Author Message
 Post subject: Strange behaviour -- get(class, id) and find() work differen
PostPosted: Thu Mar 26, 2009 1:56 am 
Newbie

Joined: Thu Jan 03, 2008 7:21 pm
Posts: 15
Hi,

I met a very strange problem with hibernate. I have a parent entity -- Employee which has two child Entities -- Comment and ManagerEmployee. Employee has one-to-many mappings to both.

When I use get(Employee.class, id), hibernate generates a huge select statement with left out join to tables of Comment and ManagerEmployee, and I got duplicated results in employee.getComments().size()

When I use find("from Employee where id = ?", id), then hibernate generates separate select statements for each Employee, Comment and ManagerEmployee tables. and there is no duplicated results in employee.getComments().size()

Did anyone meet the same problem before, can I still trust the get(class, id) method? How can get & find have different strategies when dealing with semantically same queries?

Thanks a lot for any clarify on this!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 3:34 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't know what is causing the duplicates, but the difference between session.get() and using a HQL query is that session.get() uses the fetching strategy that you have specified in your mapping files/annotations (eg. fetch="join"), but this is ignored when you use HQL. The reason that you are seeing additional select's in the second case is probably that your collections are non-lazy.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 8:54 pm 
Newbie

Joined: Thu Jan 03, 2008 7:21 pm
Posts: 15
nordborg wrote:
I don't know what is causing the duplicates, but the difference between session.get() and using a HQL query is that session.get() uses the fetching strategy that you have specified in your mapping files/annotations (eg. fetch="join"), but this is ignored when you use HQL. The reason that you are seeing additional select's in the second case is probably that your collections are non-lazy.


Thanks nordborg.

You solved my problem!

Just think it's better to let new Hibernate users know, depending on the mapping, Hibernate will generates different SQL queries which might in turn produce different results. get(class, id) is different from find("from class where id = ?", id).


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

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.