From fb0d0c5cf71a990e12ba4df22176486180424f84 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Wed, 6 Nov 2013 12:17:51 +0100 Subject: [PATCH] HHH-2907 Adding @CreationTimestamp generator annotation --- .../annotations/CreationTimestamp.java | 49 +++++++ .../tuple/CreationTimestampGeneration.java | 127 ++++++++++++++++++ .../generated/DefaultGeneratedValueTest.java | 51 +++++-- 3 files changed, 218 insertions(+), 9 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/annotations/CreationTimestamp.java create mode 100644 hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/CreationTimestamp.java b/hibernate-core/src/main/java/org/hibernate/annotations/CreationTimestamp.java new file mode 100644 index 0000000000..d10a194186 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/annotations/CreationTimestamp.java @@ -0,0 +1,49 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat, Inc. and/or its affiliates or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat, Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.annotations; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.hibernate.tuple.CreationTimestampGeneration; + +/** + * Marks a property as the creation timestamp of the containing entity. The property value will be set to the current + * VM date exactly once when saving the owning entity for the first time. + *

+ * Supported property types: + *

+ * + * @author Gunnar Morling + */ +@ValueGenerationType(generatedBy = CreationTimestampGeneration.class) +@Retention(RetentionPolicy.RUNTIME) +public @interface CreationTimestamp { +} diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java new file mode 100644 index 0000000000..040fe72fac --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java @@ -0,0 +1,127 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.tuple; + +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Date; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.annotations.CreationTimestamp; + +/** + * Value generation implementation for {@link CreationTimestamp}. + * + * @author Gunnar Morling + */ +public class CreationTimestampGeneration implements AnnotationValueGeneration { + + private ValueGenerator generator; + + @Override + public void initialize(CreationTimestamp annotation, Class propertyType) { + if ( java.sql.Date.class.isAssignableFrom( propertyType ) ) { + generator = new CreationTimestampGeneratorForSqlDate(); + } + else if ( Time.class.isAssignableFrom( propertyType ) ) { + generator = new CreationTimestampGeneratorForSqlTime(); + } + else if ( Timestamp.class.isAssignableFrom( propertyType ) ) { + generator = new CreationTimestampGeneratorForSqlTimestamp(); + } + else if ( Date.class.isAssignableFrom( propertyType ) ) { + generator = new CreationTimestampGeneratorForDate(); + } + else if ( Calendar.class.isAssignableFrom( propertyType ) ) { + generator = new CreationTimestampGeneratorForCalendar(); + } + else { + throw new HibernateException( "Unsupported property type for generator annotation @CreationTimestamp" ); + } + } + + @Override + public GenerationTiming getGenerationTiming() { + return GenerationTiming.INSERT; + } + + @Override + public ValueGenerator getValueGenerator() { + return generator; + } + + @Override + public boolean referenceColumnInSql() { + return false; + } + + @Override + public String getDatabaseGeneratedReferencedColumnValue() { + return null; + } + + private static class CreationTimestampGeneratorForDate implements ValueGenerator { + + @Override + public Date generateValue(Session session, Object owner) { + return new Date(); + } + } + + private static class CreationTimestampGeneratorForCalendar implements ValueGenerator { + + @Override + public Calendar generateValue(Session session, Object owner) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime( new Date() ); + return calendar; + } + } + + private static class CreationTimestampGeneratorForSqlDate implements ValueGenerator { + + @Override + public java.sql.Date generateValue(Session session, Object owner) { + return new java.sql.Date( new Date().getTime() ); + } + } + + private static class CreationTimestampGeneratorForSqlTime implements ValueGenerator