Skip to content

Commit dcd837c

Browse files
committed
HHH-17751 - allow @CurrentTimestamp @Version with force-increment locking
1 parent 1909769 commit dcd837c

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

hibernate-core/src/main/java/org/hibernate/generator/EventType.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ public enum EventType {
3939
*/
4040
UPDATE,
4141
/**
42-
* An event that occurs during verification of a lock of type
43-
* of {@link org.hibernate.LockMode#OPTIMISTIC_FORCE_INCREMENT}
44-
* or {@link org.hibernate.LockMode#PESSIMISTIC_FORCE_INCREMENT}.
42+
* An event that occurs when attempting to acquire
43+
* {@linkplain org.hibernate.LockMode#OPTIMISTIC_FORCE_INCREMENT optimistic}
44+
* or {@linkplain org.hibernate.LockMode#PESSIMISTIC_FORCE_INCREMENT pessimistic}
45+
* locks which trigger forcing an increment to the {@linkplain jakarta.persistence.Version version}.
4546
*/
4647
@Incubating
4748
FORCE_INCREMENT

hibernate-core/src/main/java/org/hibernate/generator/internal/CurrentTimestampGeneration.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ public class CurrentTimestampGeneration implements BeforeExecutionGenerator, OnE
8787
public static final String CLOCK_SETTING_NAME = "hibernate.testing.clock";
8888

8989
private final EnumSet<EventType> eventTypes;
90-
9190
private final JavaType<Object> propertyType;
92-
9391
private final CurrentTimestampGeneratorDelegate delegate;
92+
9493
private static final Map<Class<?>, BiFunction<@Nullable Clock, Integer, CurrentTimestampGeneratorDelegate>> GENERATOR_PRODUCERS = new HashMap<>();
9594
private static final Map<Key, CurrentTimestampGeneratorDelegate> GENERATOR_DELEGATES = new ConcurrentHashMap<>();
9695

@@ -192,11 +191,6 @@ public class CurrentTimestampGeneration implements BeforeExecutionGenerator, OnE
192191
);
193192
}
194193

195-
private static JavaType<Object> getPropertyType(GeneratorCreationContext context) {
196-
return context.getDatabase().getTypeConfiguration().getJavaTypeRegistry()
197-
.getDescriptor( context.getProperty().getType().getReturnedClass() );
198-
}
199-
200194
public CurrentTimestampGeneration(CurrentTimestamp annotation, Member member, GeneratorCreationContext context) {
201195
delegate = getGeneratorDelegate( annotation.source(), member, context );
202196
eventTypes = fromArray( annotation.event() );
@@ -268,6 +262,11 @@ public static <T extends Clock> T getClock(SessionFactory sessionFactory) {
268262
return (T) sessionFactory.getProperties().get( CLOCK_SETTING_NAME );
269263
}
270264

265+
private static JavaType<Object> getPropertyType(GeneratorCreationContext context) {
266+
return context.getDatabase().getTypeConfiguration().getJavaTypeRegistry()
267+
.getDescriptor( context.getProperty().getType().getReturnedClass() );
268+
}
269+
271270
@Override
272271
public boolean generatedOnExecution() {
273272
return delegate == null;
@@ -316,8 +315,17 @@ private record Key(Class<?> clazz, @Nullable Clock clock, int precision) {
316315

317316
static Timestamp getCurrentTimestamp(SharedSessionContractImplementor session) {
318317
final Dialect dialect = session.getJdbcServices().getJdbcEnvironment().getDialect();
319-
final boolean callable = dialect.isCurrentTimestampSelectStringCallable();
320-
final String timestampSelectString = dialect.getCurrentTimestampSelectString();
318+
return getCurrentTimestampFromDatabase(
319+
dialect.getCurrentTimestampSelectString(),
320+
dialect.isCurrentTimestampSelectStringCallable(),
321+
session
322+
);
323+
}
324+
325+
static Timestamp getCurrentTimestampFromDatabase(
326+
String timestampSelectString,
327+
boolean callable,
328+
SharedSessionContractImplementor session) {
321329
final JdbcCoordinator coordinator = session.getJdbcCoordinator();
322330
final StatementPreparer statementPreparer = coordinator.getStatementPreparer();
323331
PreparedStatement statement = null;

0 commit comments

Comments
 (0)