em.merge is not an option because I don't want a new row if the id doesn't already exist. Also, it calls an extra select.
I don't want to do find and then call merge as it will again be extra selects.
I was hoping for a way without having to write the JPQL query because I am not sure of the exact number of fields that will be updated.
Asked
Active
Viewed 581 times
1

fR0DDY
- 795
- 1
- 8
- 20
-
How do you expect to figure out if something exists if you can't have "extra" selects? – Tobb Jul 03 '19 at 15:11
-
3Have you tried `@Query(value = "update E set field = :value WHERE id = :id")` ? It won't update anything if id does not exists. – t4dohx Jul 03 '19 at 15:30
-
So you want to create a new object (unmanaged) and then try to persist it? – gel Jul 03 '19 at 16:32
-
1If you don't want to write `jpql` update query, I think extra select is unavoidable. With this approach, as you said, you need to call `entityManager.find` first and then call `update` method. But I think best solution is using jpql update query(as mentioned in the comments). Because it has not extra select overhead and also does not make any changes if data does not exist. – hamed Jul 03 '19 at 17:05
-
@hamed Yeah, I was hoping if there is a way besides writing JPQL query. – fR0DDY Jul 04 '19 at 04:34