[jboss-user] [EJB 3.0] - Re: UserTransactions and merge

hoeft do-not-reply at jboss.com
Fri Nov 17 06:07:13 EST 2006


I haven't tried it yet but here's my suggestion.
This code should work:


  | UserTransaction ut = sessionContext.getUserTransaction();
  | ut.begin();
  | MyEntity a = em.find(MyEntity.class, Long.valueOf(1)); // <-- a is bound. Not initialized lazy fields can be accessed
  | a.setName("FOO2");
  | MyRelatedBean b = a.getMyRelatedBean();
  | em.merge(a);
  | ut.commit();
  | 

It think your code isn't working, because your entity is detached after the first commit. You must bind it (with the find method) bevor you can access its lazy loaded fields. Another possibilitie is to enforce, that the field is loaded before the entity is detached. There are two ways to do this:
- access the lazy loaded field before detachment (see the code below)
- use ejb-QL (fetch join)


  | UserTransaction ut = sessionContext.getUserTransaction();
  | ut.begin();
  | MyEntity a = em.find(MyEntity.class, Long.valueOf(1));
  | a.getMyRelatedBean(); //the lazy attribut is initialized. If it is a list: call size()
  | ut.commit(); //a is being detached
  | 
  | ut = sessionContext.getUserTransaction();
  | ut.begin();
  | a.setName("FOO2");
  | MyRelatedBean b = a.getMyRelatedBean();
  | em.merge(a);
  | ut.commit();
  | 


Best regards
Meinert

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3986796#3986796

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3986796



More information about the jboss-user mailing list