Replies: 1 comment 2 replies
-
When we do |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It my be out of scope of JPA which is why I opted for discussion rather than issue.
A few but I don't think all databases would have some sort of
upsert
operation. I was wondering if there's a way of incorporating something into the standard though?Right now I would do something with this logic:
var recordToUpdateOrInsert
= find existing record usingem.createQuery
recordToUpdateOrInsert = new Entity()
recordToUpdateOrInsert
em.persist(recordToUpdateOrInsert)
recordToUpdateOrInsert
em.merge(recordToUpdateOrInsert)
em.flush()
// it will be flushed after the transaction scope finishes, but this makes it easier to focus onPersistenceException
(which handles both OptimisticLock and unique constraint errors)var recordToUpdate
= find existing record usingem.createQuery
throw new IllegalStateException("expected existing value");
recordToUpdate
em.merge(recordToUpdate)
em.flush()
// it will be flushed after the transaction scope finishes, but this makes it easier to focus onWhich is kind of verbose, though I made a function that I use with Spring Data JPA with the following signature
Beta Was this translation helpful? Give feedback.
All reactions