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?