SCJP,SCWCD,SCDJWS,SCBCD
SCJP,SCWCD,SCDJWS,SCBCD
Your explaination always helps me! ( I remembered you helped me for my last question).
right after the em.remove(x) call but before the commit() call, if we put the call em.persist(x), then the x will become managed again. Is it correct?
But if we call persist(x) after the commit(), then the EntityExistsException will be thrown, right?
The find and getReference methods are not required to be invoked within a transaction context. If an entity manager with transaction-scoped persistence context is in use, the resulting entities will be detached;
If X is a detached entity, an IllegalArgumentException will be thrown by the remove operation (or the transaction commit will fail).
SCJP5 | SCBCD5 | SCEA5 Part 1
The semantics of the persist operation, applied to an entity X are as follows:
If X is a NEW entity, it becomes MANAGED. The entity X will be entered into the database at or before transaction commit or as a result of the flush operation.
If X is a preexisting MANAGED entity, it is IGNORED by the persist operation. However, the persist operation is cascaded to entities referenced by X, if the relationships from X to these other entities is annotated with the cascade=PERSIST or cascade=ALL annotation element value or specified with the equivalent XML descriptor element.
If X is a REMOVED entity, it becomes MANAGED.
If X is a DETACHED object, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.
For all entities Y referenced by a relationship from X, if the relationship to Y has been annotated with the cascade element value cascade=PERSIST or cascade=ALL, the persist operation is applied to Y.
If X is a DETACHED object, the EntityExistsException may be thrown when the persist operation is invoked
SCJP,SCWCD,SCDJWS,SCBCD
For your tested code #1
code:
Order o=new Order();
context.getUserTransaction().begin();
o.setOrderName("S/W Order");
em.persist(o);
context.getUserTransaction().commit();
em.persist(o);
--------------------------------------------------------------------------
A TransactionRequiredException is thrown
Can you let me know is this on the last line you got the TransactionRequiredException? I got confused with MZ's note, it says
quote:If X is a DETACHED object, the EntityExistsException may be thrown when the persist operation is invoked
The persist, merge, remove, and refresh methods MUST be invoked within a transaction context
when an entity manager with a transaction-scoped persistence context is used. If there is no
transaction context, the javax.persistence.TransactionRequiredException is thrown
For your tested code #2
code:
Order o=new Order();
context.getUserTransaction().begin();
o.setOrderName("S/W Order");
em.persist(o);
em.clear();
em.persist(o);
context.getUserTransaction().commit();
--------------------------------
A PersistentObjectException is thrown.
Can you tell me which line the PersistenceObjectException is thrown? Is this on the second em.persist(o) call?
if the em.clear() is detaching the object, then next em.persist(o) call should has the same effect as your code #1, why it throws different exception?
You got style baby! More than this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|