|
15 | 15 | import java.util.Objects; |
16 | 16 | import java.util.Set; |
17 | 17 |
|
| 18 | +import org.hibernate.StaleStateException; |
18 | 19 | import org.hibernate.engine.jdbc.mutation.JdbcValueBindings; |
19 | 20 | import org.hibernate.engine.jdbc.mutation.ParameterUsage; |
20 | 21 | import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails; |
|
27 | 28 | import org.hibernate.engine.jdbc.spi.MutationStatementPreparer; |
28 | 29 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
29 | 30 | import org.hibernate.engine.spi.SharedSessionContractImplementor; |
| 31 | +import org.hibernate.exception.ConstraintViolationException; |
30 | 32 | import org.hibernate.internal.util.collections.CollectionHelper; |
31 | 33 | import org.hibernate.jdbc.Expectation; |
32 | 34 | import org.hibernate.persister.entity.mutation.EntityMutationTarget; |
|
54 | 56 | import org.hibernate.sql.model.internal.TableUpdateCustomSql; |
55 | 57 | import org.hibernate.sql.model.internal.TableUpdateStandard; |
56 | 58 |
|
| 59 | +import static org.hibernate.exception.ConstraintViolationException.ConstraintKind.UNIQUE; |
57 | 60 | import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER; |
58 | 61 |
|
59 | 62 | /** |
@@ -159,14 +162,22 @@ public void performMutation( |
159 | 162 | "Upsert update altered no rows - inserting : %s", |
160 | 163 | tableMapping.getTableName() |
161 | 164 | ); |
162 | | - performInsert( jdbcValueBindings, session ); |
| 165 | + try { |
| 166 | + performInsert( jdbcValueBindings, session ); |
| 167 | + } |
| 168 | + catch (ConstraintViolationException cve) { |
| 169 | + throw cve.getKind() == UNIQUE |
| 170 | + // assume it was the primary key constraint which was violated, |
| 171 | + // due to a new version of the row existing in the database |
| 172 | + ? new StaleStateException( mutationTarget.getRolePath() ) |
| 173 | + : cve; |
| 174 | + } |
163 | 175 | } |
164 | 176 | } |
165 | 177 | } |
166 | 178 | finally { |
167 | 179 | jdbcValueBindings.afterStatement( tableMapping ); |
168 | 180 | } |
169 | | - |
170 | 181 | } |
171 | 182 |
|
172 | 183 | private void performDelete(JdbcValueBindings jdbcValueBindings, SharedSessionContractImplementor session) { |
|
0 commit comments