unify timestamp generation in one generator instead of three
shows a nice feature of the constructor-based init
This commit is contained in:
parent
be9358e02f
commit
c595347803
|
@ -13,7 +13,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.Target;
|
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 <em>creation timestamp</em>.
|
* Specifies that the annotated field of property is a generated <em>creation timestamp</em>.
|
||||||
|
@ -28,7 +28,7 @@ import org.hibernate.tuple.CreationTimestampGeneration;
|
||||||
*
|
*
|
||||||
* @see CurrentTimestamp
|
* @see CurrentTimestamp
|
||||||
*/
|
*/
|
||||||
@ValueGenerationType(generatedBy = CreationTimestampGeneration.class)
|
@ValueGenerationType(generatedBy = CurrentTimestampGeneration.class)
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
@Target({ FIELD, METHOD })
|
@Target({ FIELD, METHOD })
|
||||||
public @interface CreationTimestamp {
|
public @interface CreationTimestamp {
|
||||||
|
|
|
@ -9,6 +9,7 @@ package org.hibernate.annotations;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import org.hibernate.tuple.CurrentTimestampGeneration;
|
||||||
import org.hibernate.tuple.GenerationTiming;
|
import org.hibernate.tuple.GenerationTiming;
|
||||||
|
|
||||||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
|
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.YearMonth}, or
|
||||||
* {@link java.time.ZonedDateTime}.
|
* {@link java.time.ZonedDateTime}.
|
||||||
*
|
*
|
||||||
* @see CurrentTimestampGeneration
|
|
||||||
*
|
|
||||||
* @author Steve Ebersole
|
|
||||||
*
|
|
||||||
* @see UpdateTimestamp
|
* @see UpdateTimestamp
|
||||||
* @see CreationTimestamp
|
* @see CreationTimestamp
|
||||||
|
* @see CurrentTimestampGeneration
|
||||||
|
*
|
||||||
|
* @since 6.0
|
||||||
|
*
|
||||||
|
* @author Steve Ebersole
|
||||||
*/
|
*/
|
||||||
@ValueGenerationType(generatedBy = CurrentTimestampGeneration.class)
|
@ValueGenerationType(generatedBy = CurrentTimestampGeneration.class)
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
|
|
|
@ -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<CurrentTimestamp> {
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -7,10 +7,14 @@
|
||||||
package org.hibernate.annotations;
|
package org.hibernate.annotations;
|
||||||
|
|
||||||
import jakarta.persistence.AttributeConverter;
|
import jakarta.persistence.AttributeConverter;
|
||||||
|
import org.hibernate.Remove;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
|
*
|
||||||
|
* @deprecated this class is no longer used
|
||||||
*/
|
*/
|
||||||
|
@Deprecated(since= "6.2") @Remove
|
||||||
public class NoAttributeConverter<O,R> implements AttributeConverter<O,R> {
|
public class NoAttributeConverter<O,R> implements AttributeConverter<O,R> {
|
||||||
@Override
|
@Override
|
||||||
public R convertToDatabaseColumn(Object attribute) {
|
public R convertToDatabaseColumn(Object attribute) {
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
package org.hibernate.annotations;
|
package org.hibernate.annotations;
|
||||||
|
|
||||||
import org.hibernate.Remove;
|
import org.hibernate.Remove;
|
||||||
|
import org.hibernate.tuple.SourceGeneration;
|
||||||
|
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
|
@ -13,7 +13,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.Target;
|
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 <em>update timestamp.</em>
|
* Specifies that the annotated field of property is a generated <em>update timestamp.</em>
|
||||||
|
@ -28,7 +28,7 @@ import org.hibernate.tuple.UpdateTimestampGeneration;
|
||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
*/
|
*/
|
||||||
@ValueGenerationType(generatedBy = UpdateTimestampGeneration.class)
|
@ValueGenerationType(generatedBy = CurrentTimestampGeneration.class)
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
@Target({ FIELD, METHOD })
|
@Target({ FIELD, METHOD })
|
||||||
public @interface UpdateTimestamp {
|
public @interface UpdateTimestamp {
|
||||||
|
|
|
@ -19,7 +19,7 @@ import java.util.Properties;
|
||||||
import org.hibernate.AssertionFailure;
|
import org.hibernate.AssertionFailure;
|
||||||
import org.hibernate.FetchMode;
|
import org.hibernate.FetchMode;
|
||||||
import org.hibernate.annotations.Source;
|
import org.hibernate.annotations.Source;
|
||||||
import org.hibernate.annotations.SourceGeneration;
|
import org.hibernate.tuple.SourceGeneration;
|
||||||
import org.hibernate.annotations.SourceType;
|
import org.hibernate.annotations.SourceType;
|
||||||
import org.hibernate.boot.MappingException;
|
import org.hibernate.boot.MappingException;
|
||||||
import org.hibernate.boot.jaxb.Origin;
|
import org.hibernate.boot.jaxb.Origin;
|
||||||
|
|
|
@ -21,7 +21,7 @@ import static org.hibernate.annotations.UuidGenerator.Style.TIME;
|
||||||
import static org.hibernate.internal.util.ReflectHelper.getPropertyType;
|
import static org.hibernate.internal.util.ReflectHelper.getPropertyType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UUID-based IdentifierGenerator
|
* Generates {@link UUID}s.
|
||||||
*
|
*
|
||||||
* @see org.hibernate.annotations.UuidGenerator
|
* @see org.hibernate.annotations.UuidGenerator
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -6,17 +6,21 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.tuple;
|
package org.hibernate.tuple;
|
||||||
|
|
||||||
|
import org.hibernate.Internal;
|
||||||
|
import org.hibernate.Remove;
|
||||||
import org.hibernate.annotations.CreationTimestamp;
|
import org.hibernate.annotations.CreationTimestamp;
|
||||||
import org.hibernate.annotations.SourceType;
|
import org.hibernate.annotations.SourceType;
|
||||||
import org.hibernate.dialect.Dialect;
|
import org.hibernate.dialect.Dialect;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Value generation implementation for {@link CreationTimestamp}.
|
* Value generation implementation for {@link CreationTimestamp}, no longer used.
|
||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
*
|
*
|
||||||
* @see org.hibernate.annotations.CurrentTimestampGeneration
|
* @deprecated use {@link CurrentTimestampGeneration}
|
||||||
*/
|
*/
|
||||||
|
@Internal
|
||||||
|
@Deprecated(since = "6.2") @Remove
|
||||||
public class CreationTimestampGeneration implements AnnotationValueGeneration<CreationTimestamp> {
|
public class CreationTimestampGeneration implements AnnotationValueGeneration<CreationTimestamp> {
|
||||||
|
|
||||||
private ValueGenerator<?> generator;
|
private ValueGenerator<?> generator;
|
||||||
|
|
|
@ -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}.
|
||||||
|
* <p>
|
||||||
|
* 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,20 +4,16 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
* 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
|
* 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.AssertionFailure;
|
||||||
import org.hibernate.Internal;
|
import org.hibernate.Internal;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.annotations.Source;
|
||||||
import org.hibernate.dialect.Dialect;
|
import org.hibernate.dialect.Dialect;
|
||||||
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
|
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
|
||||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||||
import org.hibernate.internal.CoreMessageLogger;
|
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 org.jboss.logging.Logger;
|
||||||
|
|
||||||
import java.lang.reflect.Member;
|
import java.lang.reflect.Member;
|
||||||
|
@ -40,12 +36,11 @@ import static org.hibernate.tuple.GenerationTiming.ALWAYS;
|
||||||
*
|
*
|
||||||
* @author Gavin King
|
* @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")
|
@Deprecated(since = "6.2")
|
||||||
@Internal
|
@Internal
|
||||||
public class SourceGeneration
|
public class SourceGeneration implements InMemoryGenerator, ValueGenerator<Object> {
|
||||||
implements InMemoryGenerator, ValueGenerator<Object> {
|
|
||||||
|
|
||||||
private static final CoreMessageLogger log = Logger.getMessageLogger(
|
private static final CoreMessageLogger log = Logger.getMessageLogger(
|
||||||
CoreMessageLogger.class,
|
CoreMessageLogger.class,
|
|
@ -6,17 +6,21 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.tuple;
|
package org.hibernate.tuple;
|
||||||
|
|
||||||
|
import org.hibernate.Internal;
|
||||||
|
import org.hibernate.Remove;
|
||||||
import org.hibernate.annotations.SourceType;
|
import org.hibernate.annotations.SourceType;
|
||||||
import org.hibernate.annotations.UpdateTimestamp;
|
import org.hibernate.annotations.UpdateTimestamp;
|
||||||
import org.hibernate.dialect.Dialect;
|
import org.hibernate.dialect.Dialect;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Value generation implementation for {@link UpdateTimestamp}.
|
* Value generation implementation for {@link UpdateTimestamp}, no longer used.
|
||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
*
|
*
|
||||||
* @see org.hibernate.annotations.CurrentTimestampGeneration
|
* @deprecated use {@link CurrentTimestampGeneration}
|
||||||
*/
|
*/
|
||||||
|
@Internal
|
||||||
|
@Deprecated(since = "6.2") @Remove
|
||||||
public class UpdateTimestampGeneration implements AnnotationValueGeneration<UpdateTimestamp> {
|
public class UpdateTimestampGeneration implements AnnotationValueGeneration<UpdateTimestamp> {
|
||||||
|
|
||||||
private ValueGenerator<?> generator;
|
private ValueGenerator<?> generator;
|
||||||
|
|
|
@ -9,6 +9,7 @@ package org.hibernate.tuple;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
|
import org.hibernate.Internal;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.annotations.GeneratorType;
|
import org.hibernate.annotations.GeneratorType;
|
||||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
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}.
|
* An {@link AnnotationValueGeneration} which delegates to a {@link ValueGenerator}.
|
||||||
|
* Underlies the {@link GeneratorType} annotation.
|
||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
|
*
|
||||||
|
* @deprecated since {@link GeneratorType} is deprecated
|
||||||
*/
|
*/
|
||||||
public class VmValueGeneration
|
@Internal
|
||||||
implements InMemoryGenerator {
|
@Deprecated(since = "6.2")
|
||||||
|
public class VmValueGeneration implements InMemoryGenerator {
|
||||||
|
|
||||||
private final GenerationTiming generationTiming;
|
private final GenerationTiming generationTiming;
|
||||||
private final ValueGenerator<?> generator;
|
private final ValueGenerator<?> generator;
|
||||||
|
|
Loading…
Reference in New Issue