From 8de4ff2a3a6a30541d463fdb373e1e8f0de953b5 Mon Sep 17 00:00:00 2001 From: dewarder Date: Sun, 28 Aug 2016 00:25:44 +0300 Subject: [PATCH] HHH-10406 - Provide @CreationTimestamp and @UpdateTimestamp for Java Time --- .../tuple/CreationTimestampGeneration.java | 25 +--- .../hibernate/tuple/TimestampGenerators.java | 125 +++++++++++++----- .../tuple/UpdateTimestampGeneration.java | 25 +--- .../generated/DefaultGeneratedValueTest.java | 69 ++++++++++ 4 files changed, 162 insertions(+), 82 deletions(-) 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 80f9c6d13f..c9b5d5d31c 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/CreationTimestampGeneration.java @@ -6,12 +6,6 @@ */ 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.annotations.CreationTimestamp; /** @@ -25,24 +19,7 @@ public class CreationTimestampGeneration implements AnnotationValueGeneration propertyType) { - if ( java.sql.Date.class.isAssignableFrom( propertyType ) ) { - generator = new TimestampGenerators.CurrentSqlDateGenerator(); - } - else if ( Time.class.isAssignableFrom( propertyType ) ) { - generator = new TimestampGenerators.CurrentSqlTimeGenerator(); - } - else if ( Timestamp.class.isAssignableFrom( propertyType ) ) { - generator = new TimestampGenerators.CurrentSqlTimestampGenerator(); - } - else if ( Date.class.isAssignableFrom( propertyType ) ) { - generator = new TimestampGenerators.CurrentDateGenerator(); - } - else if ( Calendar.class.isAssignableFrom( propertyType ) ) { - generator = new TimestampGenerators.CurrentCalendarGenerator(); - } - else { - throw new HibernateException( "Unsupported property type for generator annotation @CreationTimestamp" ); - } + generator = TimestampGenerators.get(propertyType); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/TimestampGenerators.java b/hibernate-core/src/main/java/org/hibernate/tuple/TimestampGenerators.java index 117f478875..828bfb8b17 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/TimestampGenerators.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/TimestampGenerators.java @@ -8,57 +8,114 @@ package org.hibernate.tuple; import java.sql.Time; import java.sql.Timestamp; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.MonthDay; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.time.Year; +import java.time.YearMonth; +import java.time.ZonedDateTime; import java.util.Calendar; import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; -import org.hibernate.Session; +import org.hibernate.HibernateException; /** * Generators for obtaining the current VM timestamp in different representations. * * @author Gunnar Morling */ -/* package */ interface TimestampGenerators { +/* package */ final class TimestampGenerators { - class CurrentDateGenerator implements ValueGenerator { + private static final Map, ValueGenerator> generators; - @Override - public Date generateValue(Session session, Object owner) { - return new Date(); - } + private TimestampGenerators() { } - class CurrentCalendarGenerator implements ValueGenerator { + static { + generators = new HashMap<>(); + generators.put( + Date.class, + (session, owner) -> new Date() + ); + generators.put( + Calendar.class, + (session, owner) -> { + Calendar calendar = Calendar.getInstance(); + calendar.setTime( new Date() ); + return calendar; + } + ); + generators.put( + java.sql.Date.class, + (session, owner) -> new java.sql.Date( System.currentTimeMillis() ) + ); - @Override - public Calendar generateValue(Session session, Object owner) { - Calendar calendar = Calendar.getInstance(); - calendar.setTime( new Date() ); - return calendar; - } + generators.put( + Time.class, + (session, owner) -> new Time( System.currentTimeMillis() ) + ); + generators.put( + Timestamp.class, + (session, owner) -> new Timestamp( System.currentTimeMillis() ) + ); + generators.put( + Instant.class, + (session, owner) -> Instant.now() + ); + generators.put( + LocalDate.class, + (session, owner) -> LocalDate.now() + ); + generators.put( + LocalDateTime.class, + (session, owner) -> LocalDateTime.now() + ); + generators.put( + LocalTime.class, + (session, owner) -> LocalTime.now() + ); + generators.put( + MonthDay.class, + (session, owner) -> MonthDay.now() + ); + generators.put( + OffsetDateTime.class, + (session, owner) -> OffsetDateTime.now() + ); + generators.put( + OffsetTime.class, + (session, owner) -> OffsetTime.now() + ); + generators.put( + Year.class, + (session, owner) -> Year.now() + ); + generators.put( + YearMonth.class, + (session, owner) -> YearMonth.now() + ); + generators.put( + ZonedDateTime.class, + (session, owner) -> ZonedDateTime.now() + ); } - class CurrentSqlDateGenerator implements ValueGenerator { - - @Override - public java.sql.Date generateValue(Session session, Object owner) { - return new java.sql.Date( new Date().getTime() ); + @SuppressWarnings("unchecked") + public static ValueGenerator get(final Class type) { + final ValueGenerator valueGeneratorSupplier = generators.get( + type ); + if ( Objects.isNull( valueGeneratorSupplier ) ) { + throw new HibernateException( + "Unsupported property type [" + type.getName() + "] for @CreationTimestamp or @UpdateTimestamp generator annotation" ); } - } - class CurrentSqlTimeGenerator implements ValueGenerator