-->
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: Dynamic association fetching...
PostPosted: Sun Dec 26, 2004 2:49 pm 
Newbie

Joined: Sun Dec 26, 2004 2:18 pm
Posts: 1
Location: india
Hello Everyone..
I have a requirement where i want to make use of hibernate lazy loading feature for associations i have as well as want to dynamically override the same to load all the associations based on a parameter to the method which fetches the data. I tried using setFetchMode(association, FetchMode.EAGER) but here there is a restriction that i can load only one one-to-one and only one one-to-many association. But my objects have more than one associations of a single type. Any clues on how can i achieve the same. Is there are possibility to override at run time hibernates lazy loading feature and make it to load all the association. Any help in this regards would be appreciated.

Thanks..
hejain
Hibernate version:
2.x

Mapping documents:


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
MySQL
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

_________________
Leveraging Open Source Technology...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 26, 2004 6:30 pm 
Beginner
Beginner

Joined: Sun Oct 03, 2004 8:50 am
Posts: 32
Location: McLean, VA
I've had this need before as well. I wrote a very simple util class to take care of this. The class has a set of static integers (powers of 2) that represent all the possible associations on a particular POJO. Then I have a method that takes the POJO whose associations are to be intialized and an integer which represents all the association that need to be initialized. Here's an example:

Code:
public class ModelPrepUtil {

    /**
     * Represents the violation types component of an enforcement.
     */
    public final static int VIOLATIONS_TYPES = 1;
   
    /**
     * Represents the relief component of an enforcement.
     */
    public final static int RELIEFS = 2;
   
    /**
     * Represents the defendants/respondants types component of an enforcement.
     */
    public final static int DEFENDANTS_RESPONDANTS = 4;
   
    /**
     * Represents the environmental justices component of an enforcement.
     */
    public final static int ENVIRONMENTAL_JUSTICES = 8;

    public final static void initalizeSelectedComponents(Enforcement p_enforcement, int p_initializedAggregates){
        if(p_enforcement == null || p_initializedAggregates <= 0){
            return;
        }
       
        try{
            if ((p_initializedAggregates & VIOLATIONS_TYPES) > 0) {
                Hibernate.initialize(p_enforcement.getViolationTypes());
            }

            if ((p_initializedAggregates & RELIEFS) > 0) {
                Hibernate.initialize(p_enforcement.getReliefs());
            }

            if ((p_initializedAggregates & DEFENDANTS_RESPONDANTS) > 0) {
                Hibernate.initialize(p_enforcement.getDefendantRespondants());
            }

            if ((p_initializedAggregates & ENVIRONMENTAL_JUSTICES) > 0) {
                Hibernate.initialize(p_enforcement.getEnvironmentalJustices());
            }
        }catch(HibernateException e){
           //do something
        }
    }
}


I happened to write one for each of our major data model components as we only have 6 or so of them, but this model could fairly easily be generalized if you had a whole lot POJOs to deal with like this.

_________________
- Chad


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.