-->
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.  [ 2 posts ] 
Author Message
 Post subject: Could some please answer these question ?
PostPosted: Sat Jul 28, 2007 2:23 am 
Newbie

Joined: Sat Jul 28, 2007 2:06 am
Posts: 3
Hi
I am new to Hibernate. I just started coding.

Here is my scenario,
I have a table A and which has primary key (say k1), there is an associated table B where the primary key is a composite key( say k2)
k1 + timestamp +type creates the the primary key of table B ie, k2.

Now the tables are already in place and I am now incorporating Hibernate into this.
Here is my question.

For each row in table A there can be 0 or upto 6 rows in table B.
I have designed the HBM.xml and classes for table A to have an associatin set, of table B and the key is composite key and generator class is assigned.In this set declaration i mentioned lazy = true.

I have written Equals() and Hashcode() for classesfor table B.
(B 's objects object 1 = object 2 if the k1 and timestamp and type are equal)


My requirement is to load all rows from table A, with the corresponding set of rows from B where the number of rows inB is exactly 6.

so I have used the query to load all A's where setB.size =6;

Now I have doubt that this defeated the lazy loading cocept since I checked for the size of the set it loaded into the memory during the execution of the above mentioned query.

Can anyone please explain is that true / ho can I identify from the logs if all the rows are loaded on execution of the query itself. ?

Thanks
SKS[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 28, 2007 8:36 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Are you saying you're loading all the A objects into memory then using java to test setB.size() = 6??

If so, hibernate will need to load the B's into memory before doing the test - not the most efficient method.

or...

Are you putting setB.size = 6 in the query?? This is a much better way. Hibernate will generate SQL that counts the B's and only returns A's that have 6 B's.

Additionally, because the B relationship is lazy, it won't load the B's until you access the collection (or force loading through initialize).

If you're going to detach the A objects from the session and want the B's to be populated automatically you can add a "left join fetch" to your HQL query to eagerly load the B's:
"from A as a left join fetch a.setB where a.setB.size = 6"

Turn on show_sql in the hibernate config to see the SQL statements that hibernate generates.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.