-->
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.  [ 5 posts ] 
Author Message
 Post subject: Right way to retrieve "many" end of one-to-many ma
PostPosted: Fri Sep 12, 2003 10:23 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
Hi, guys:

We have a birdirectional one-to-many association between department and stident. We are trying to retrieve a list of students based on a given department. The following is the code we wrote to do this job. This code works, but we are wondering if this kind of coding has a downside and affect performance. Or is there a better way to do it? Really appreciate your inputs.

Session session = .......
session.update(department);
Hibernate.initialize(department);
List attr = new ArrayList(department.getStudents());
session.close();
return attr;

regards,


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 10:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
>> Hibernate.initialize(department);

Why do you have this line?


Anyway, I would write this code as:


Session session = .......
session.lock(department, LockMode.NONE);
Hibernate.initialize( department.getStudents() );
session.close();
return department.getStudents();


at least in Hibernate 2.1.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 11:00 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
Really appreciate your input, Gavin:

A question: why do we need to use LockMode here if no lock required
(LockMode.NONE). Whats the benefit of it?

regards,



gavin wrote:
>> Hibernate.initialize(department);

Why do you have this line?


Anyway, I would write this code as:


Session session = .......
session.lock(department, LockMode.NONE);
Hibernate.initialize( department.getStudents() );
session.close();
return department.getStudents();


at least in Hibernate 2.1.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 11:04 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
Sorry, forgot to ask you about :

Hibernate.initialize(department);

This line is used to force init of a persistable object. We do not need it?


gavin wrote:
>> Hibernate.initialize(department);

Why do you have this line?


Anyway, I would write this code as:


Session session = .......
session.lock(department, LockMode.NONE);
Hibernate.initialize( department.getStudents() );
session.close();
return department.getStudents();


at least in Hibernate 2.1.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 11:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
lock(foo, LockMode.NONE) just means reassociate an object with the session, w/o hitting the db at all.

Quote:
This line is used to force init of a persistable object. We do not need it?


No, of course not. Hibernate transparently initializes associations as you navigate them!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.