I have a Foo object that havs an List with one or more Bars objects. When Im trying to run Foo.getBars() Im getting LazyInitializationException. Here is my settings.
Hibernate version:
jboss-4.0.5.GA (is or is based on Hibernate 3, right?)
Mapping documents:
Foo:
Code:
@OneToMany(mappedBy="foo",targetEntity=Bar.class)
public List<Bar> getBars() {...}
Bar:
Code:
@ManyToOne(optional=false,fetch=FetchType.LAZY)
@JoinColumn(name="foo_id",referencedColumnName="id")
public Foo getFoo() {...}
Im getting (on runtime) Error:
Code:
LazyInitializationException: failed to lazily initialize a collection of role: com.foobar.fooList, no session or session was closed.
Here is how I get my EntityManager in Handlers:
Code:
@PersistenceContext(unitName="FooBarContext")
private EntityManager em;
However I have seprate Handelers for Foo & Bar. Its that ok? Or should I make one Handeler for every Entity in my Application.
I know whats the problem, when Im retreving my Foo its dosent load all the Bars. This can be "overried" by FetchType.EAGAR. However it dosent work in my case, Im getting NullPointer on my Bar.Hashcode(). And would like to solve it "lazy" way first.
Name and version of the database you are using:
Mysql 4.0.18
Thanks for your time!