Skip to content

Commit 0edd307

Browse files
gavinkingmbellade
authored andcommitted
HHH-19522 report failed optimistic lock checking for upsert()
- also fix the problem for upsert emulation with UPDATE/INSERT
1 parent 0005b80 commit 0edd307

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

hibernate-core/src/main/java/org/hibernate/sql/model/jdbc/OptionalTableUpdateOperation.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Objects;
1616
import java.util.Set;
1717

18+
import org.hibernate.StaleStateException;
1819
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings;
1920
import org.hibernate.engine.jdbc.mutation.ParameterUsage;
2021
import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails;
@@ -27,6 +28,7 @@
2728
import org.hibernate.engine.jdbc.spi.MutationStatementPreparer;
2829
import org.hibernate.engine.spi.SessionFactoryImplementor;
2930
import org.hibernate.engine.spi.SharedSessionContractImplementor;
31+
import org.hibernate.exception.ConstraintViolationException;
3032
import org.hibernate.internal.util.collections.CollectionHelper;
3133
import org.hibernate.jdbc.Expectation;
3234
import org.hibernate.persister.entity.mutation.EntityMutationTarget;
@@ -54,6 +56,7 @@
5456
import org.hibernate.sql.model.internal.TableUpdateCustomSql;
5557
import org.hibernate.sql.model.internal.TableUpdateStandard;
5658

59+
import static org.hibernate.exception.ConstraintViolationException.ConstraintKind.UNIQUE;
5760
import static org.hibernate.sql.model.ModelMutationLogging.MODEL_MUTATION_LOGGER;
5861

5962
/**
@@ -159,14 +162,22 @@ public void performMutation(
159162
"Upsert update altered no rows - inserting : %s",
160163
tableMapping.getTableName()
161164
);
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+
}
163175
}
164176
}
165177
}
166178
finally {
167179
jdbcValueBindings.afterStatement( tableMapping );
168180
}
169-
170181
}
171182

172183
private void performDelete(JdbcValueBindings jdbcValueBindings, SharedSessionContractImplementor session) {

hibernate-core/src/test/java/org/hibernate/orm/test/stateless/UpsertVersionedTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import jakarta.persistence.Entity;
44
import jakarta.persistence.Id;
55
import jakarta.persistence.Version;
6-
import org.hibernate.StaleObjectStateException;
6+
import org.hibernate.StaleStateException;
77
import org.hibernate.testing.orm.junit.DomainModel;
88
import org.hibernate.testing.orm.junit.SessionFactory;
99
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@@ -56,7 +56,7 @@ public class UpsertVersionedTest {
5656
} );
5757
fail();
5858
}
59-
catch (StaleObjectStateException sose) {
59+
catch (StaleStateException sse) {
6060
//expected
6161
}
6262
scope.inStatelessTransaction( s-> {

0 commit comments

Comments
 (0)