-->
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: Eagerly loading collections
PostPosted: Mon Jul 30, 2007 2:35 pm 
Newbie

Joined: Tue Apr 03, 2007 11:40 am
Posts: 7
Location: London
I have the following class:

public class Project {
private List<Organisation> organisations;
private List<Person> contacts;

I have lazy loading turned on for performance reasons. If I get a single project by it's ID though, i'd like to eagerly load the collections of organisations and contacts. Something like...

<query name="Project.getById">
from Project as p
left join fetch p.organisations
left join fetch p.contacts
where p.id = :projectId
</query>

Using the above HQL and then calling project.getOrganisations(), I'm getting duplicate records, due to the fact that my HQL returns a cartesian product (I think).

I am assuming that I could get around this using something like fetch="subselect" in my collection mapping - but I don't want any eager loading turned on by default.

So - is there a way that I gan eagerly load everything related to Project, but only when a 'getById' happens (i.e. only when a single entity is loaded) ?

_________________
----
Matt


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 5:24 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You could use a query to load the object allowing you to 'join fetch' the collections:
Code:
  Project project = (Project )session.createCriteria(Project .class)
  .setFetchMode("organisations", FetchMode.JOIN)
  .setFetchMode("contacts", FetchMode.JOIN)
  .add(Restrictions.eq("id", projectId)).uniqueResult();

The uniqueResult() call eliminates the duplicate records from the result set. "id" will be substituted by hibernate in SQL for the primary key of your project table.

Alternatively, load the object with load() then initialise the collections with Hibernate.initialize(project). This will issue subselects for the collections but might be more efficient than creating the cartesian product of project*organisations*contacts.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 6:48 am 
Newbie

Joined: Tue Apr 03, 2007 11:40 am
Posts: 7
Location: London
Thanks for the help, load() ing is the way to go for me I think.

_________________
----
Matt


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.