• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doing an Update with JPA

 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 38
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic