From c595347803645dff1d4272150ea4cd53c0187598 Mon Sep 17 00:00:00 2001 From: Gavin Date: Thu, 1 Dec 2022 14:39:39 +0100 Subject: [PATCH] unify timestamp generation in one generator instead of three shows a nice feature of the constructor-based init --- .../annotations/CreationTimestamp.java | 4 +- .../annotations/CurrentTimestamp.java | 10 +- .../CurrentTimestampGeneration.java | 62 ------------ .../annotations/NoAttributeConverter.java | 4 + .../org/hibernate/annotations/Source.java | 1 + .../annotations/UpdateTimestamp.java | 4 +- .../source/internal/hbm/ModelBinder.java | 2 +- .../org/hibernate/id/uuid/UuidGenerator.java | 2 +- .../tuple/CreationTimestampGeneration.java | 8 +- .../tuple/CurrentTimestampGeneration.java | 96 +++++++++++++++++++ .../SourceGeneration.java | 13 +-- .../tuple/UpdateTimestampGeneration.java | 8 +- .../hibernate/tuple/VmValueGeneration.java | 9 +- 13 files changed, 136 insertions(+), 87 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/annotations/CurrentTimestampGeneration.java create mode 100644 hibernate-core/src/main/java/org/hibernate/tuple/CurrentTimestampGeneration.java rename hibernate-core/src/main/java/org/hibernate/{annotations => tuple}/SourceGeneration.java (91%) diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/CreationTimestamp.java b/hibernate-core/src/main/java/org/hibernate/annotations/CreationTimestamp.java index 8fca7be0ac..3160d777ec 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/CreationTimestamp.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/CreationTimestamp.java @@ -13,7 +13,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Retention; import java.lang.annotation.Target; -import org.hibernate.tuple.CreationTimestampGeneration; +import org.hibernate.tuple.CurrentTimestampGeneration; /** * Specifies that the annotated field of property is a generated creation timestamp. @@ -28,7 +28,7 @@ import org.hibernate.tuple.CreationTimestampGeneration; * * @see CurrentTimestamp */ -@ValueGenerationType(generatedBy = CreationTimestampGeneration.class) +@ValueGenerationType(generatedBy = CurrentTimestampGeneration.class) @Retention(RUNTIME) @Target({ FIELD, METHOD }) public @interface CreationTimestamp { diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/CurrentTimestamp.java b/hibernate-core/src/main/java/org/hibernate/annotations/CurrentTimestamp.java index 49613fda09..38e9d8edc2 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/CurrentTimestamp.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/CurrentTimestamp.java @@ -9,6 +9,7 @@ package org.hibernate.annotations; import java.lang.annotation.Retention; import java.lang.annotation.Target; +import org.hibernate.tuple.CurrentTimestampGeneration; import org.hibernate.tuple.GenerationTiming; import static java.lang.annotation.ElementType.ANNOTATION_TYPE; @@ -50,12 +51,13 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; * {@link java.time.YearMonth}, or * {@link java.time.ZonedDateTime}. * - * @see CurrentTimestampGeneration - * - * @author Steve Ebersole - * * @see UpdateTimestamp * @see CreationTimestamp + * @see CurrentTimestampGeneration + * + * @since 6.0 + * + * @author Steve Ebersole */ @ValueGenerationType(generatedBy = CurrentTimestampGeneration.class) @Retention(RUNTIME) diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/CurrentTimestampGeneration.java b/hibernate-core/src/main/java/org/hibernate/annotations/CurrentTimestampGeneration.java deleted file mode 100644 index 6c1c191087..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/annotations/CurrentTimestampGeneration.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later - * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html - */ -package org.hibernate.annotations; - -import org.hibernate.Internal; -import org.hibernate.dialect.Dialect; -import org.hibernate.tuple.AnnotationValueGeneration; -import org.hibernate.tuple.GenerationTiming; -import org.hibernate.tuple.TimestampGenerators; -import org.hibernate.tuple.ValueGenerator; - -/** - * Value generation strategy using the database {@code current_timestamp} - * function or the JVM current instant. - * - * @see CurrentTimestamp - * - * @author Steve Ebersole - */ -@Internal -public class CurrentTimestampGeneration implements AnnotationValueGeneration { - private GenerationTiming timing; - private ValueGenerator generator; - - @Override - public void initialize(CurrentTimestamp annotation, Class propertyType) { - if ( annotation.source() == SourceType.VM ) { - // ValueGenerator is only used for in-VM generation - generator = TimestampGenerators.get( propertyType ); - } - timing = annotation.timing(); - } - - @Override - public GenerationTiming getGenerationTiming() { - return timing; - } - - @Override - public ValueGenerator getValueGenerator() { - return generator; - } - - @Override - public boolean referenceColumnInSql() { - return true; - } - - @Override - public String getDatabaseGeneratedReferencedColumnValue() { - return "current_timestamp"; - } - - @Override - public String getDatabaseGeneratedReferencedColumnValue(Dialect dialect) { - return dialect.currentTimestamp(); - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/NoAttributeConverter.java b/hibernate-core/src/main/java/org/hibernate/annotations/NoAttributeConverter.java index 80f7b36f06..6d830464f4 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/NoAttributeConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/NoAttributeConverter.java @@ -7,10 +7,14 @@ package org.hibernate.annotations; import jakarta.persistence.AttributeConverter; +import org.hibernate.Remove; /** * @author Steve Ebersole + * + * @deprecated this class is no longer used */ +@Deprecated(since= "6.2") @Remove public class NoAttributeConverter implements AttributeConverter { @Override public R convertToDatabaseColumn(Object attribute) { diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/Source.java b/hibernate-core/src/main/java/org/hibernate/annotations/Source.java index 8620132505..50235c5f8b 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/Source.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/Source.java @@ -7,6 +7,7 @@ package org.hibernate.annotations; import org.hibernate.Remove; +import org.hibernate.tuple.SourceGeneration; import java.lang.annotation.Retention; import java.lang.annotation.Target; diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/UpdateTimestamp.java b/hibernate-core/src/main/java/org/hibernate/annotations/UpdateTimestamp.java index b746a19c96..12f80fe919 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/UpdateTimestamp.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/UpdateTimestamp.java @@ -13,7 +13,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Retention; import java.lang.annotation.Target; -import org.hibernate.tuple.UpdateTimestampGeneration; +import org.hibernate.tuple.CurrentTimestampGeneration; /** * Specifies that the annotated field of property is a generated update timestamp. @@ -28,7 +28,7 @@ import org.hibernate.tuple.UpdateTimestampGeneration; * * @author Gunnar Morling */ -@ValueGenerationType(generatedBy = UpdateTimestampGeneration.class) +@ValueGenerationType(generatedBy = CurrentTimestampGeneration.class) @Retention(RUNTIME) @Target({ FIELD, METHOD }) public @interface UpdateTimestamp { diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java index 624a985f07..6937abee32 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java @@ -19,7 +19,7 @@ import java.util.Properties; import org.hibernate.AssertionFailure; import org.hibernate.FetchMode; import org.hibernate.annotations.Source; -import org.hibernate.annotations.SourceGeneration; +import org.hibernate.tuple.SourceGeneration; import org.hibernate.annotations.SourceType; import org.hibernate.boot.MappingException; import org.hibernate.boot.jaxb.Origin; diff --git a/hibernate-core/src/main/java/org/hibernate/id/uuid/UuidGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/uuid/UuidGenerator.java index bb713f94d4..270b10bb35 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/uuid/UuidGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/uuid/UuidGenerator.java @@ -21,7 +21,7 @@ import static org.hibernate.annotations.UuidGenerator.Style.TIME; import static org.hibernate.internal.util.ReflectHelper.getPropertyType; /** - * UUID-based IdentifierGenerator + * Generates {@link UUID}s. * * @see org.hibernate.annotations.UuidGenerator */ diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java index c7ce464e29..626551e32d 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java @@ -6,17 +6,21 @@ */ package org.hibernate.tuple; +import org.hibernate.Internal; +import org.hibernate.Remove; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.SourceType; import org.hibernate.dialect.Dialect; /** - * Value generation implementation for {@link CreationTimestamp}. + * Value generation implementation for {@link CreationTimestamp}, no longer used. * * @author Gunnar Morling * - * @see org.hibernate.annotations.CurrentTimestampGeneration + * @deprecated use {@link CurrentTimestampGeneration} */ +@Internal +@Deprecated(since = "6.2") @Remove public class CreationTimestampGeneration implements AnnotationValueGeneration { private ValueGenerator generator; diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/CurrentTimestampGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/CurrentTimestampGeneration.java new file mode 100644 index 0000000000..b578f2a604 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/tuple/CurrentTimestampGeneration.java @@ -0,0 +1,96 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.tuple; + +import org.hibernate.AssertionFailure; +import org.hibernate.Internal; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.CurrentTimestamp; +import org.hibernate.annotations.SourceType; +import org.hibernate.annotations.UpdateTimestamp; +import org.hibernate.dialect.Dialect; +import org.hibernate.internal.util.ReflectHelper; + +import java.lang.reflect.Member; + +import static org.hibernate.tuple.GenerationTiming.ALWAYS; +import static org.hibernate.tuple.GenerationTiming.INSERT; + +/** + * Value generation strategy which produces a timestamp using the database + * {@link Dialect#currentTimestamp() current_timestamp} function or the JVM + * {@linkplain java.time.Clock#instant() current instant}. + *

+ * Underlies the {@link CurrentTimestamp}, {@link CreationTimestamp}, and + * {@link UpdateTimestamp} annotations. + * + * @see CurrentTimestamp + * @see CreationTimestamp + * @see UpdateTimestamp + * + * @since 6.0 + * + * @author Steve Ebersole + * @author Gavin King + */ +@Internal +public class CurrentTimestampGeneration implements ValueGeneration { + private final GenerationTiming timing; + private final ValueGenerator generator; + + public CurrentTimestampGeneration(CurrentTimestamp annotation, Member member, GeneratorCreationContext context) { + generator = getGenerator( annotation.source(), member ); + timing = annotation.timing(); + } + + public CurrentTimestampGeneration(CreationTimestamp annotation, Member member, GeneratorCreationContext context) { + generator = getGenerator( annotation.source(), member ); + timing = INSERT; + } + + public CurrentTimestampGeneration(UpdateTimestamp annotation, Member member, GeneratorCreationContext context) { + generator = getGenerator( annotation.source(), member ); + timing = ALWAYS; + } + + private static ValueGenerator getGenerator(SourceType source, Member member) { + switch (source) { + case VM: + // ValueGenerator is only used for in-VM generation + return TimestampGenerators.get( ReflectHelper.getPropertyType( member ) ); + case DB: + return null; + default: + throw new AssertionFailure("unknown source"); + } + } + + @Override + public GenerationTiming getGenerationTiming() { + return timing; + } + + @Override + public ValueGenerator getValueGenerator() { + return generator; + } + + @Override + public boolean referenceColumnInSql() { + return true; + } + + @Override + public String getDatabaseGeneratedReferencedColumnValue() { + return "current_timestamp"; + } + + @Override + public String getDatabaseGeneratedReferencedColumnValue(Dialect dialect) { + return dialect.currentTimestamp(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/SourceGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/SourceGeneration.java similarity index 91% rename from hibernate-core/src/main/java/org/hibernate/annotations/SourceGeneration.java rename to hibernate-core/src/main/java/org/hibernate/tuple/SourceGeneration.java index 74b8625fe3..2e4869bd2d 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/SourceGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/SourceGeneration.java @@ -4,20 +4,16 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html */ -package org.hibernate.annotations; +package org.hibernate.tuple; import org.hibernate.AssertionFailure; import org.hibernate.Internal; import org.hibernate.Session; +import org.hibernate.annotations.Source; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.spi.JdbcCoordinator; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.internal.CoreMessageLogger; -import org.hibernate.tuple.GenerationTiming; -import org.hibernate.tuple.GeneratorCreationContext; -import org.hibernate.tuple.InMemoryGenerator; -import org.hibernate.tuple.TimestampGenerators; -import org.hibernate.tuple.ValueGenerator; import org.jboss.logging.Logger; import java.lang.reflect.Member; @@ -40,12 +36,11 @@ import static org.hibernate.tuple.GenerationTiming.ALWAYS; * * @author Gavin King * - * @deprecated because {@link Source} is generated, though this implementation is instructive + * @deprecated because {@link Source} is deprecated, though this implementation is instructive */ @Deprecated(since = "6.2") @Internal -public class SourceGeneration - implements InMemoryGenerator, ValueGenerator { +public class SourceGeneration implements InMemoryGenerator, ValueGenerator { private static final CoreMessageLogger log = Logger.getMessageLogger( CoreMessageLogger.class, diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/UpdateTimestampGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/UpdateTimestampGeneration.java index 588c036ce4..b56417d6f9 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/UpdateTimestampGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/UpdateTimestampGeneration.java @@ -6,17 +6,21 @@ */ package org.hibernate.tuple; +import org.hibernate.Internal; +import org.hibernate.Remove; import org.hibernate.annotations.SourceType; import org.hibernate.annotations.UpdateTimestamp; import org.hibernate.dialect.Dialect; /** - * Value generation implementation for {@link UpdateTimestamp}. + * Value generation implementation for {@link UpdateTimestamp}, no longer used. * * @author Gunnar Morling * - * @see org.hibernate.annotations.CurrentTimestampGeneration + * @deprecated use {@link CurrentTimestampGeneration} */ +@Internal +@Deprecated(since = "6.2") @Remove public class UpdateTimestampGeneration implements AnnotationValueGeneration { private ValueGenerator generator; diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/VmValueGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/VmValueGeneration.java index 030ec113e4..3ef580336e 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/VmValueGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/VmValueGeneration.java @@ -9,6 +9,7 @@ package org.hibernate.tuple; import java.lang.reflect.Constructor; import org.hibernate.HibernateException; +import org.hibernate.Internal; import org.hibernate.Session; import org.hibernate.annotations.GeneratorType; import org.hibernate.engine.spi.SharedSessionContractImplementor; @@ -17,11 +18,15 @@ import static org.hibernate.internal.util.ReflectHelper.getDefaultConstructor; /** * An {@link AnnotationValueGeneration} which delegates to a {@link ValueGenerator}. + * Underlies the {@link GeneratorType} annotation. * * @author Gunnar Morling + * + * @deprecated since {@link GeneratorType} is deprecated */ -public class VmValueGeneration - implements InMemoryGenerator { +@Internal +@Deprecated(since = "6.2") +public class VmValueGeneration implements InMemoryGenerator { private final GenerationTiming generationTiming; private final ValueGenerator generator;