From 1e39c4b07fe53f63ac32ee538df1bfeafa52252f Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Wed, 6 Nov 2013 12:18:21 +0100 Subject: [PATCH] HHH-2907 Adding @UpdateTimestamp generator annotation --- .../annotations/UpdateTimestamp.java | 49 +++++++++++ .../tuple/CreationTimestampGeneration.java | 53 ++---------- .../hibernate/tuple/TimestampGenerators.java | 81 ++++++++++++++++++ .../tuple/UpdateTimestampGeneration.java | 84 +++++++++++++++++++ .../generated/DefaultGeneratedValueTest.java | 50 ++++++++++- 5 files changed, 268 insertions(+), 49 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/annotations/UpdateTimestamp.java create mode 100644 hibernate-core/src/main/java/org/hibernate/tuple/TimestampGenerators.java create mode 100644 hibernate-core/src/main/java/org/hibernate/tuple/UpdateTimestampGeneration.java diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/UpdateTimestamp.java b/hibernate-core/src/main/java/org/hibernate/annotations/UpdateTimestamp.java new file mode 100644 index 0000000000..db584bce48 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/annotations/UpdateTimestamp.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.UpdateTimestampGeneration; + +/** + * Marks a property as the update timestamp of the containing entity. The property value will be set to the current VM + * date whenever the owning entity is updated. + *

+ * Supported property types: + *

+ * + * @author Gunnar Morling + */ +@ValueGenerationType(generatedBy = UpdateTimestampGeneration.class) +@Retention(RetentionPolicy.RUNTIME) +public @interface UpdateTimestamp { +} 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 040fe72fac..4c5f61d8c1 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java @@ -29,7 +29,6 @@ import java.util.Calendar; import java.util.Date; import org.hibernate.HibernateException; -import org.hibernate.Session; import org.hibernate.annotations.CreationTimestamp; /** @@ -44,19 +43,19 @@ public class CreationTimestampGeneration implements AnnotationValueGeneration propertyType) { if ( java.sql.Date.class.isAssignableFrom( propertyType ) ) { - generator = new CreationTimestampGeneratorForSqlDate(); + generator = new TimestampGenerators.CurrentSqlDateGenerator(); } else if ( Time.class.isAssignableFrom( propertyType ) ) { - generator = new CreationTimestampGeneratorForSqlTime(); + generator = new TimestampGenerators.CurrentSqlTimeGenerator(); } else if ( Timestamp.class.isAssignableFrom( propertyType ) ) { - generator = new CreationTimestampGeneratorForSqlTimestamp(); + generator = new TimestampGenerators.CurrentSqlTimestampGenerator(); } else if ( Date.class.isAssignableFrom( propertyType ) ) { - generator = new CreationTimestampGeneratorForDate(); + generator = new TimestampGenerators.CurrentDateGenerator(); } else if ( Calendar.class.isAssignableFrom( propertyType ) ) { - generator = new CreationTimestampGeneratorForCalendar(); + generator = new TimestampGenerators.CurrentCalendarGenerator(); } else { throw new HibernateException( "Unsupported property type for generator annotation @CreationTimestamp" ); @@ -82,46 +81,4 @@ public class CreationTimestampGeneration implements AnnotationValueGeneration { - - @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