aspose file tools*
The moose likes Object Relational Mapping and the fly likes Doing an Update with JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Doing an Update with JPA" Watch "Doing an Update with JPA" New topic
Author

Doing an Update with JPA

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1054
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: 1054
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doing an Update with JPA