-->
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: Collections and Bi-directional relationship
PostPosted: Tue Jul 27, 2004 4:12 am 
Newbie

Joined: Wed Jul 14, 2004 8:17 am
Posts: 4
Location: London (UK)
Hi

I've just started using Hibernate so this may appear as a naive question.

Let say I have 2 data objects: Server and Log.
a Server can have many logs and a Log belongs to one Server only.

Server has a serverId (sequence in PostgreSQL) and a Set getLogs() method. The method getLogs() uses lazy loading.
Log has a logId (sequence in PostgreSQL) and a Server getServer() method.

Now what's the best or recommended (for performance reason or correctness) way to create a new Log?
a)
Code:
server.getLogs().add(new Log());
session.update(server);


b)
Code:
Log log = new Log();
log.setServer(server);
session.save(log);


Also my understanding is that once a Server object is retrieved from the db and providing we're in session a call to getLogs() will lazy load the Set of Logs. Isn't that a performance hit when at some point the Set of Logs for a Server object will be huge?
I could probably have the same result by retrieving all Logs belonging to a Server with a from... where query.

It's far less obvious that I thought to break away from SQLish thinking ;)

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 6:11 am 
Regular
Regular

Joined: Tue Oct 28, 2003 8:25 am
Posts: 72
Location: Belgium
For correctness:

Code:
Log log = new Log();
log.setServer(server);
server.getLogs().add(log);


For performance, this is far less obvious. Problem with this code is that adding a new log will load the entire set. If you are going to have lots of logs, I would personally not map this relation as bidirectional but only from the many-to-one side. Have a look at the docs chapter 14.

About the call to getLogs(), you are nearly correct: it is only when you call a method of the returned set that the collection will be loaded.


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.