| Author |
Doing an Update with JPA
|
Mike London
Ranch Hand
Joined: Jul 12, 2002
Posts: 951
|
|
Hello,
To do an Update using JPA/Hibernate, would you use the merge method of the EntityManager class?
Could you also write an UPDATE statement and then execute that query somehow to do the update that way instead?
New to JPA so I'm trying to figure out how best to do it.
Thanks,
mike
|
 |
Vijay Vishwa
Greenhorn
Joined: Nov 21, 2010
Posts: 28
|
|
When using JPA let the EntityManager to manage the entity objects.
EntityManger.merge should be used.
Also calling merge method explicity is not required, this because:
When using Container Managed Persistance context, the lifecycle of entity bean is managed by EM.
That means any changes in the state of entity will be persisted by the EM in DB at the end of transaction even if merge is not called.
Following is the sample program.
|
SCJP 5.0 OCBCD 5.0(Formerly SCBCD)
|
 |
Mike London
Ranch Hand
Joined: Jul 12, 2002
Posts: 951
|
|
Vijay Vishwa wrote:When using JPA let the EntityManager to manage the entity objects.
EntityManger.merge should be used.
Also calling merge method explicity is not required, this because:
When using Container Managed Persistance context, the lifecycle of entity bean is managed by EM.
That means any changes in the state of entity will be persisted by the EM in DB at the end of transaction even if merge is not called.
Following is the sample program.
Vijay,
That's an awesome explanation!!! The code is a perfect example to compliment your explanation and is easy to follow.
I'm going to try this code on Monday.
Thanks again very much for your terrific reply.
mike
|
 |
 |
|
|
subject: Doing an Update with JPA
|
|
|