Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » entity.getAlist() reopen de emFactory and entityManager that was previously closed!!(even in a relation marked as lazy and Cascade.Detach, is this correct?)
icon5.gif  entity.getAlist() reopen de emFactory and entityManager that was previously closed!! [message #527533] Thu, 15 April 2010 14:26 Go to next message
No real name is currently offline No real nameFriend
Messages: 7
Registered: April 2010
Junior Member
Hello guys!
Please could anyone give me some explanation about this?

I have a entity that have a collection of other entity. This relationship is marked as :
@OneToMany(mappedBy="xxx",fetch=FetchType.LAZY,cascade={CascadeType.DETACH})

then I create a factory and an entitymanager:

emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME );
em = emf.createEntityManager();

after I load an instance of this entity:

entity= em.find(MyEntity.class, 123L);

after that I detach this entity and close the em and emf. I test all of this using:
em.contains(entity)
em.isOpen()
emf.isOpen()

After all of this I try to itirate over the association list of my entity, like this:
for(MyEntity2 e:entity.getMyEntities2()){
print...
}

At this moment the prompt console show that the eclipselink create a new connection, and do a query to load these objects!!!
Is this a good idea? I will always get an association list even in a detached and closed instance?

I believe this feature isn't part of specification and can work differently in others implementations...

What do you think about this?
Thanks!!!
Re: entity.getAlist() reopen de emFactory and entityManager that was previously closed!! [message #528141 is a reply to message #527533] Mon, 19 April 2010 14:03 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

EclipseLink does support access to LAZY relationships after the EntityManager has been closed. This is undefined in the JPA spec, but it a very desirable feature.

I'm not sure what you mean by detach, if you serialize the object, it will not be able to access LAZY relationships.

If you close the EntityManagerFactory, I would expect an error, this is odd. Is there something else holding onto the same EntityManagerFactory? If there is, then close will not actually close it.


James : Wiki : Book : Blog : Twitter
Re: entity.getAlist() reopen de emFactory and entityManager that was previously closed!! [message #528249 is a reply to message #528141] Mon, 19 April 2010 19:41 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 7
Registered: April 2010
Junior Member
Thanks for the answer James!

For detach I mean call the detach() method from entityManager.

I don't know if there are something holding the EntityManagerFactory but before close I call entityManager.clear() to detach all entities and after I test if the EntityManagerFactory is open:

emf.isOpen();

and the return is false.

But even now my entity can load the lazy associations creating a new connection and doing the queries. This isn't a problem for me, but open a opportunity to the programmer work in an inappropriate way.

[Updated on: Mon, 19 April 2010 20:07]

Report message to a moderator

Re: entity.getAlist() reopen de emFactory and entityManager that was previously closed!! [message #533567 is a reply to message #527533] Fri, 14 May 2010 15:03 Go to previous message
No real name is currently offline No real nameFriend
Messages: 7
Registered: April 2010
Junior Member
I'm working with with app managed jpa 2.0. That is the case:

There are two entities A and B. A has a list of B.
I load an A, detach and close entityManager.

Then I iterate over A.Bs.

Now I change some value in B and commit.

I reload A and list Bs. The new value don't come back!

If I restart the program or do a listAll directly over B then I get the correct A.Bs list.

How Is the best way to work on this?
Previous Topic:JPA query that returns LAST (youngest) entity (pk index) for ANY table
Next Topic:First Level Cache in EntityManager
Goto Forum:
  


Current Time: Fri Apr 26 14:23:59 GMT 2024

Powered by FUDForum. Page generated in 0.03469 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top