-->
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.  [ 1 post ] 
Author Message
 Post subject: reattachment of detached objects / lazy loading
PostPosted: Wed Dec 06, 2006 1:05 pm 
Regular
Regular

Joined: Sat May 20, 2006 3:49 am
Posts: 78
Hibernate version:
3.2.1 GA
Name and version of the database you are using:
Oracle 10g XE

Hello!

I have read a lot documentation on EJB 3.0, but have a few basic questions about lazy loading/reattachement of objects:

1. Is it true that EJB 3.0 doesn't support reattachment of detached objects? I thought with a simple find I could reselect my object from database. If my object was modified, I reattach that object with a merge. The object get attached to the persistence context and updated to database and is reattached. Is that right?

2. When I load my objects, no properties of Type FetchType.LAZY are loaded. Thats ok. When I need my objects from a one-to-may relationship, I reattach my detached object and fetch the related objects with a for-loop. This approach is also described in PRO EJB 3.0 from Mike Keith. Is that ok?

Now I have played around with my Business Object.

This doesn't load my Addresses:
Code:
    public static Patient loadPatient(Patient p) {
        final PersistenceContext tx = new PersistenceContext();
        try {
            tx.begin();
            p = (Patient)new PatientDAO(tx).findByObject(p);
            p.getAddresses();
            tx.commit();
           
            return p;
        } catch (Exception e) {
            log.error(e.getMessage());
            if (tx.isActive()) {
                tx.rollback();
            }
            return null;
        } finally {
            tx.close();
        }
    }


If I insert a for-loop, the addresses are loaded:
Code:
    public static Patient loadPatient(Patient p) {
        final PersistenceContext tx = new PersistenceContext();
        try {
            tx.begin();
            p = (Patient)new PatientDAO(tx).findByObject(p);
            p.getAddresses();
            for (Address a : p.getAddresses()) {
                //do nothing
            }
            tx.commit();
           
            return p;
        } catch (Exception e) {
            log.error(e.getMessage());
            if (tx.isActive()) {
                tx.rollback();
            }
            return null;
        } finally {
            tx.close();
        }
    }


Is that the correct way?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.