-->
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: Eager fetch question
PostPosted: Wed Dec 07, 2005 6:31 am 
Newbie

Joined: Wed Dec 07, 2005 6:24 am
Posts: 4
I have been working through the tutorial installed by Hibernate 3.1rc3.
In section 1.3.4 Collection of values, I learned how to modifiy value typed objects:

Code:
private void addEmailToPerson(Long personId, String emailAddress) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Person aPerson = (Person) session.load(Person.class, personId);

        // The getEmailAddresses() might trigger a lazy load of the collection
        aPerson.getEmailAddresses().add(emailAddress);

        session.getTransaction().commit();
}


How can I tune this query using eager fetch?

Thanks a lot,
Dennis


Top
 Profile  
 
 Post subject: This is what I tried
PostPosted: Thu Dec 08, 2005 3:21 am 
Newbie

Joined: Wed Dec 07, 2005 6:24 am
Posts: 4
With the code below I get a null for aPerson even though apropriate entries in the database exist (with correct ids):

Code:
private void addEmailToPerson(Long personId, String emailAddress) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Person aPerson = (Person) session.createQuery("from Person p left join fetch p.emailAddresses where p.id = :pid").setParameter("pid", personId).uniqueResult(); // Eager fetch the collection so we can use it detached
        session.getTransaction().commit();
       
       aPerson.getEmailAddresses().add(emailAddress); // aPerson (and its collection) is detached
       
       Session session2 = HibernateUtil.getSessionFactory().getCurrentSession();
       session2.beginTransaction();
       session2.update(aPerson); // Reattachment of aPerson
       session2.getTransaction().commit();
    }


Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 5:44 am 
Newbie

Joined: Sat Nov 19, 2005 10:10 pm
Posts: 9
You should look at SQL queries, that hibernate generates.
Add <property name="show_sql">true</property> to your hibernate.cfg.xml
file. Try to execute these queries manually and maybe you understand what's happens.


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