-
1. Re: How to create lazy collections
ffray Mar 16, 2005 10:45 AM (in response to elmosca2)Hi Bruno,
got the same problem! Havent found any hint on that.
Perhaps the provided version of Hibernate is a bit buggy.
I'll watch this topic and we'll see if somebody got a solution.
Greetz
Florian -
2. Re: How to create lazy collections
bill.burke Mar 16, 2005 11:14 AM (in response to elmosca2)How are you interacting with this object. If the object has been detached from persistence storage, then it would no longer be able to resolve the relationship.
The object would be detached if you are accessing it outside the transaction it was created, found, or merged.
You need to reattach the object by calling em.merge() or by refinding it. -
3. Re: How to create lazy collections
bill.burke Mar 16, 2005 11:15 AM (in response to elmosca2)FYI, this is smilar behavior to EJB 2.1. Relationships must be accessed within the context of a transaction.
-
4. Re: How to create lazy collections
elmosca2 Mar 16, 2005 11:45 AM (in response to elmosca2)But then how can I access the children of the lazy collection if I am outside the bean that contains the injected Entity Manager? Is there the possibility to get the EntityManager from outside the bean directly from the context using jndi? If so, how? I have tried several lookups without success...
Thanks, -
5. Re: How to create lazy collections
bill.burke Mar 16, 2005 11:52 AM (in response to elmosca2)You mean in a remote client? No...EntityManager cannot be access remotely.
On the server side you'll have to pre fetch the relationship in your Query or traverse the relationship while the object is still attached to persistence management.
Although the behavior is quite different, this limitation is no different than EJB 2.1. Something like this cannot be supported because these are POJOs and need to act like POJOs.
Bill -
6. Re: How to create lazy collections
elmosca2 Mar 16, 2005 12:08 PM (in response to elmosca2)Thanks Bill, I take the point. I realize that my mind is too hibernate-shaped and I want to control sessions and factories everywhere. I will just have to adapt my logic to this structure...
-
7. Re: How to create lazy collections
bill.burke Mar 16, 2005 12:20 PM (in response to elmosca2)No no...I think I may be misinterpreting your posts here. You should think in Hibernate...Eventually, once it is cleared up, you'll be able to access the EntityManager anywhere.
I may be wrong, but your current usage of LAZY loading wouldn't work with a pure Hibernate application as well unless you reattached the object on the remote client with a local Session. This is what I was trying to get at when I responded to you.
Huh...Am I making sense at all?
Bill -
8. Re: How to create lazy collections
elmosca2 Mar 16, 2005 12:37 PM (in response to elmosca2)Hehe, don't worry, one thing I have to improve eventually is my ability to communicate. All that I was trying to do figure is how to access the EntityManager, as I do with SessionFactories (configured in jboss-service.xml files). For instance, I can get a SessionFactory from a servlet using jndi and I was trying to do something similar with the EntityManager... I don't know if this is the way, because all that I want to do is to create a local session (in a servlet, for instance) to access the ejb3/hibernate3 entities that I have in a ejb3 file somewhere in the server...
I don't know if I explain myself ok,
Thanks!
Bruno -
9. Re: How to create lazy collections
diederick.verweij Mar 2, 2006 8:14 AM (in response to elmosca2)I ran into this same problem. (LazyInitializationException: failed to lazily initialize a collection - no session or session was closed ..)
The solution for my problem was the removal of a targetEntity at one end of an association.
This example describes the problem cause:
object A {
@OneToMany(mappedBy="a", targetEntity=A.class)
List as;
}
Object B {
@ManyToOne(mappedBy="b", targetEntity=B.class)
}
The targetEntity givven in B (reffering to A) causes the Exception.
It probably causes a circular fetching problem (or whatever..)
Diederick Verweij. -
10. Re: How to create lazy collections
epbernard Mar 2, 2006 4:32 PM (in response to elmosca2)@ManyToOne(mappedBy="b", targetEntity=B.class)
never ever compiled