23

I am doing some CRUD operations using JPA. For updating an object which is the right way to do?

Through update query or through the find method of EntityManager?
I have one Employee object I need to update. Which is the right way?

Please guide me.

Mohit Gupta
  • 33
  • 1
  • 5
user414967
  • 4,997
  • 6
  • 33
  • 58

3 Answers3

36

Using executeUpdate() on the Query API is faster because it bypasses the persistent context .However , by-passing persistent context would cause the state of instance in the memory and the actual values of that record in the DB are not synchronized.

Consider the following example :

 Employee employee= (Employee)entityManager.find(Employee.class , 1);
 entityManager
     .createQuery("update Employee set name = \'xxxx\' where id=1")
     .executeUpdate();

After flushing, the name in the DB is updated to the new value but the employee instance in the memory still keeps the original value .You have to call entityManager.refresh(employee) to reload the updated name from the DB to the employee instance.It sounds strange if your codes still have to manipulate the employee instance after flushing but you forget to refresh() the employee instance as the employee instance still contains the original values.

Normally , executeUpdate() is used in the bulk update process as it is faster due to bypassing the persistent context

The right way to update an entity is that you just set the properties you want to updated through the setters and let the JPA to generate the update SQL for you during flushing instead of writing it manually.

   Employee employee= (Employee)entityManager.find(Employee.class ,1);
   employee.setName("Updated Name");
Paaske
  • 4,195
  • 1
  • 19
  • 31
Ken Chan
  • 64,456
  • 22
  • 117
  • 138
7

That depends on what you want to do, but as you said, getting an entity reference using find() and then just updating that entity is the easiest way to do that.

I'd not bother about performance differences of the various methods unless you have strong indications that this really matters.

Thomas
  • 80,843
  • 12
  • 111
  • 143
  • Can we use find() to get the obj aObj and then use aObj = new MyObj(//...) to update the object? I didn't see example like this anywhere – Weishi Z Aug 12 '15 at 06:32
3

It depends on number of entities which are going to be updated, if you have large number of entities using JPA Query Update statement is better as you dont have to load all the entities from database, if you are going to update just one entity then using find and update is fine.

mprabhat
  • 19,229
  • 7
  • 42
  • 62
  • 1
    In case of bulk updates. But this also breaks isolation between code and database - most advandtage of JPA – Konstantin Pribluda Nov 29 '11 at 08:17
  • yes it does but then there are instances where you cannot fetch all records and then update them, it is not the acceptable solution in given context, so its usage depends on the context. Also when I said Update Query I meant "JPA Update Query" not "SQL Update Query" so you are still pretty much database independent. – mprabhat Nov 29 '11 at 08:22
  • JPA update query is translated to SQL query - and it still breaks object abstraction. But there are legitimate use cases – Konstantin Pribluda Nov 29 '11 at 08:29