|
12 | 12 | import java.util.Locale; |
13 | 13 | import java.util.Objects; |
14 | 14 |
|
| 15 | +import org.hibernate.StaleStateException; |
15 | 16 | import org.hibernate.engine.jdbc.mutation.JdbcValueBindings; |
16 | 17 | import org.hibernate.engine.jdbc.mutation.ParameterUsage; |
17 | 18 | import org.hibernate.engine.jdbc.mutation.internal.JdbcValueDescriptorImpl; |
|
23 | 24 | import org.hibernate.engine.jdbc.spi.JdbcServices; |
24 | 25 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
25 | 26 | import org.hibernate.engine.spi.SharedSessionContractImplementor; |
| 27 | +import org.hibernate.exception.ConstraintViolationException; |
26 | 28 | import org.hibernate.internal.util.collections.CollectionHelper; |
27 | 29 | import org.hibernate.jdbc.Expectation; |
28 | 30 | import org.hibernate.persister.entity.mutation.EntityMutationTarget; |
|
48 | 50 | import org.hibernate.sql.model.internal.TableUpdateCustomSql; |
49 | 51 | import org.hibernate.sql.model.internal.TableUpdateStandard; |
50 | 52 |
|
| 53 | +import static org.hibernate.exception.ConstraintViolationException.ConstraintKind.UNIQUE; |
51 | 54 | import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER; |
52 | 55 |
|
53 | 56 | /** |
@@ -149,7 +152,16 @@ public void performMutation( |
149 | 152 | "Upsert update altered no rows - inserting : %s", |
150 | 153 | tableMapping.getTableName() |
151 | 154 | ); |
152 | | - performInsert( jdbcValueBindings, session ); |
| 155 | + try { |
| 156 | + performInsert( jdbcValueBindings, session ); |
| 157 | + } |
| 158 | + catch (ConstraintViolationException cve) { |
| 159 | + throw cve.getKind() == UNIQUE |
| 160 | + // assume it was the primary key constraint which was violated, |
| 161 | + // due to a new version of the row existing in the database |
| 162 | + ? new StaleStateException( mutationTarget.getRolePath(), cve ) |
| 163 | + : cve; |
| 164 | + } |
153 | 165 | } |
154 | 166 | } |
155 | 167 | } |
|
0 commit comments