[jboss-user] [EJB 3.0] - Re: failed to lazily initialize a collection of role:

Wolfgang Knauf do-not-reply at jboss.com
Sat Aug 11 11:00:25 EDT 2007


Hi ! 

Probably you want to access a relationship property when the entity bean is no longer under control of the entity manager (a "detached" entity).

There are two ways around this problem:
a) set the fetch type to "EAGER"
@OneToMany(mappedBy="...", fetch=FetchType.EAGER)
  | public Collection<MyChild> getChilds()
This causes the entity manager to load the child collection when the parent is loaded. May be a big performance problem.

b) prefetch your childs when the entity bean is still under entity manager control.
Imagine a find method of a session bean:
public MyParent findById (Integer id)
  | {
  |    MyParent parent = this.entityManager.find(MyParent.class, id);
  |    parent.getChilds().size();
  | } 
"parent.getChilds().size();" forces loading of the childen.

Hope this helps

Wolfgang

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

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



More information about the jboss-user mailing list