-->
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.  [ 4 posts ] 
Author Message
 Post subject: Selectively populating collections for mapped Objects
PostPosted: Sun Sep 16, 2007 11:16 pm 
Newbie

Joined: Sun Sep 16, 2007 10:22 pm
Posts: 3
I am new to Hibernate and have spent the last several days going through documentation and tutorials, and I am having difficulty understanding exactly how it is possible to to only fetch data for certain collections for a particular mapped object.

Take the following example:

Let's say I have a table called PROJECTS, which has many-to-many relationships with 20 other tables, all of which I would like to specify as <set> elements in my mapping. I want to be able to select data for projects without always selecting data for all of the other 20 tables that it has many-to-many relationships with. Sometimes I might only want the Project data, sometimes I might also want data from 1 or more of the related tables. In my Project POJO, I would potentially have collection objects available to represent the data from the 20 related tables, but I would like to be able to selectively choose (preferably using HQL) which of these collections I actually want populated. Can this problem be solved with only one mapping file and one POJO?

Unless I have misunderstood the Hibernate documentation (entirely possible), then I thought it was possible use the fetch="join" attribute to be able to populate a collection only when that data is requested. However, I believe the documentation also said that the fetch="join" attribute is ignored when running HQL queries. I would preferably like to be able to use HQL, unless it is just not possible.

I have searched to see how other's have solved these types of problems, and it seems that an alternate strategy is to use subclasses, where the collections are defined in the subclasses. For example, if I wanted to select contacts for a project, I would make a subclass of Project called ProjectContacts, which would contain a collection to store the contact data. However, I am not particularly fond of using the subclass approach to solve this problem, as it seems like it could potentially lead to having to create an extremely large number of classes to represent data that logically seems better defined by a single class.

I am very interested to see how others have solved this issue in their own projects.

Thanks,

_________________
Justin Holzer


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 10:58 am 
Newbie

Joined: Mon Jun 11, 2007 5:45 am
Posts: 8
I think HQL would be one option, where you could load the needed data by passing it into a specific constructor for you Project Object.

Another option could be lazy loading. Have you looked into that yet?


Top
 Profile  
 
 Post subject: Lazy Loading
PostPosted: Mon Sep 17, 2007 11:16 am 
Newbie

Joined: Sun Sep 16, 2007 10:22 pm
Posts: 3
Yes, I have read a good deal of documentation about lazy loading, but to be honest, I have had a tough time up to this point understanding exactly how it works.

If it will allow me to, in a sense, "activate" my many-to-many relationships only at the times when I specifically want to use them, then it would be perfect. From what I have read, I have not seen if this is possible or not. I will continue reading about lazy loading to try and gain a better understanding.

Is lazy loading enabled by default in Hibernate 3 or does it have to be explicitly enabled? If so, does it have to be enabled globally ( in the Hibernate config) and in the mapping files as well?

As for setting up different constructors, that is an interesting idea, but I thought Hibernate only used the default, no-argument constructor? If not, how could one go about specifying a particular constructor to be called for each different many-to-many relationship?

I was also reading about inheritance mapping techniques, such as Table per Subclass, but the examples I have seen seem to use this as more of a way as representing subsets of data, then for using it to represent relationships betwen two or more tables.

Thanks,

_________________
Justin Holzer


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 2:50 pm 
Newbie

Joined: Sun Sep 16, 2007 10:22 pm
Posts: 3
After doing a bit more reading, I think I have a better understanding of lazy loading. Please correct me if any of the below assumptions are incorrect.

- By default, Hibernate 3 will enable lazy loading/fetching for collections.

- If lazy loading is enabled for a collection, that collection can be explicitly populated in one of the following ways:
  1. Calling the get() method for the associated collection within the context of a transaction (ie. project.getContacts() )
  2. Using HQL to do an inner/outer fetch join.


I think my problem is that I was using the Hibernate Tools eclipse plugin to run HQL queries against my mappings, and that when I go to view the results, and I expand the collections, it populates the data at that time. I will have to run more tests by simply analyzing the return values in my java code to make sure my understanding is correct.

Thanks,

_________________
Justin Holzer


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