-->
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: does Hibernate getter always makes call to database
PostPosted: Thu Mar 10, 2011 6:17 am 
Newbie

Joined: Thu Mar 10, 2011 6:10 am
Posts: 2
I am new to Hibernate/JPA. This is probably a very simple question. I have got class
@Entity
class University {

List<Department> departments = new ArrayList<Department>();

public List<Department> getDepartments() {
return departments;
}

}
Just wondering everytime when i make call to University.getDepartments(), Will Hibernate every time makes a Database call or picks from session?


Top
 Profile  
 
 Post subject: Re: does Hibernate getter always makes call to database
PostPosted: Thu Mar 10, 2011 6:39 am 
Newbie

Joined: Fri Feb 08, 2008 11:10 am
Posts: 3
Only once, assuming you invoke the method twice on the same physical object instance.

..assuming normal conditions, of course. You can of course write a query or map your object so no extra db call is needed, or retrieve your University object within different Hibernate Session/EntityManages instances causing several db calls etc.

/Jens


Top
 Profile  
 
 Post subject: Re: does Hibernate getter always makes call to database
PostPosted: Thu Mar 10, 2011 6:42 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
If you call just the getter of a collection, nothing will happen until you access the collection (e.g. size() or get()). Then hibernate will query the association unless it is not already initialized (e.g. you have eagerly selected it via your mapping or HQL).

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: does Hibernate getter always makes call to database
PostPosted: Thu Mar 10, 2011 7:30 am 
Newbie

Joined: Thu Mar 10, 2011 6:10 am
Posts: 2
Many Thanks for replying...

I basically need to print number of Departments in University

so when i am saving new Department i got two ways ,

1. After saving i can get University.getDepartment.size().
Is above a better approach or

2. int number;

save() {

session.save(department)
number++;
}

and print number.
Its a high volume website where i am implementing this.
Please suggest which approach is better


Top
 Profile  
 
 Post subject: Re: does Hibernate getter always makes call to database
PostPosted: Thu Mar 10, 2011 8:15 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Where is your save-method implemented?

You should implement this functionality using a named query:
Code:
select count(d) from University u join u.department d where ...

_________________
-----------------
Need advanced help? http://www.viada.eu


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.