1

Question:
PersistenceUtil.isLoaded is evaluated by looking at which EntityManager or is it L2 Cache? After all Entities are loaded by only one of these.

PersistenceUtil.isLoaded method definition says:
"Determine the load state of an entity. This method can be used to determine the load state of an entity passed as a reference." But there is no mention of loaded where?

4
  • It looks at the Entity Manager, see very good explanation: stackoverflow.com/questions/10437552/… Commented Apr 29, 2013 at 6:26
  • Which EntityManager? It does not taken any EntityManager as argument, so which EntityManager is it looking at - thats the question :( Commented Apr 29, 2013 at 6:51
  • Do you have more then one persistence unit defined in persistence.xml? Commented Apr 29, 2013 at 7:00
  • No, only one persistence unit. Commented Apr 29, 2013 at 8:43

2 Answers 2

1

I've checked the source code with the implementation of this interface. Looks like isLoaded is going over all defined persistent providers and checks if entity is attached to any of them.

As a result you can't know to which EntityManager your entity is bound.

Sign up to request clarification or add additional context in comments.

2 Comments

So, essentially, it checks if entity is loaded in/by ANY EntityManager? Since a entity object is always associated with One EntityManager, PersistenceUtil.isLoaded(entity) may return True, but Entity may still not be in the EntityManager you are dealing with. This renders the isLoaded method not very useful. Or maybe i misunderstood?
Yes, it seems you are right. This method simply verifies one thing: will the given objects state be changed once I change its property or not. Which database is used, which EntityManager is used is irrelevant.
1

Entities in JPA are attached to an entity manager, and there's plumbing to help you with that - see f.e. clear(), contains(), merge() in EntityManager alone.

Usually entities are attached to the EntityManager that created them. It's not a good idea to mix entities from different EntityManagers.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.