HHH-10406 - Provide @CreationTimestamp and @UpdateTimestamp for Java Time

This commit is contained in:
dewarder 2016-08-28 00:25:44 +03:00 committed by Vlad Mihalcea
parent fec5afc8b6
commit 8de4ff2a3a
4 changed files with 162 additions and 82 deletions

View File

@ -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<Cr
@Override
public void initialize(CreationTimestamp annotation, Class<?> 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

View File

@ -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<Date> {
private static final Map<Class<?>, ValueGenerator<?>> generators;
@Override
public Date generateValue(Session session, Object owner) {
return new Date();
}
private TimestampGenerators() {
}
class CurrentCalendarGenerator implements ValueGenerator<Calendar> {
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<java.sql.Date> {
@Override
public java.sql.Date generateValue(Session session, Object owner) {
return new java.sql.Date( new Date().getTime() );
@SuppressWarnings("unchecked")
public static <T> ValueGenerator<T> get(final Class<T> 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<Time> {
@Override
public Time generateValue(Session session, Object owner) {
return new Time( new Date().getTime() );
}
}
class CurrentSqlTimestampGenerator implements ValueGenerator<Timestamp> {
@Override
public Timestamp generateValue(Session session, Object owner) {
return new Timestamp( new Date().getTime() );
}
return (ValueGenerator<T>) valueGeneratorSupplier;
}
}

View File

@ -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.UpdateTimestamp;
/**
@ -25,24 +19,7 @@ public class UpdateTimestampGeneration implements AnnotationValueGeneration<Upda
@Override
public void initialize(UpdateTimestamp annotation, Class<?> 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 @UpdateTimestamp" );
}
generator = TimestampGenerators.get(propertyType);
}
@Override

View File

@ -8,6 +8,16 @@ package org.hibernate.test.generated;
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 javax.persistence.Column;
@ -59,6 +69,17 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
assertNull( theEntity.vmCreatedSqlDate );
assertNull( theEntity.vmCreatedSqlTime );
assertNull( theEntity.vmCreatedSqlTimestamp );
assertNull( theEntity.vmCreatedSqlLocalDate );
assertNull( theEntity.vmCreatedSqlLocalTime );
assertNull( theEntity.vmCreatedSqlLocalDateTime );
assertNull( theEntity.vmCreatedSqlMonthDay );
assertNull( theEntity.vmCreatedSqlOffsetDateTime );
assertNull( theEntity.vmCreatedSqlOffsetTime );
assertNull( theEntity.vmCreatedSqlYear );
assertNull( theEntity.vmCreatedSqlYearMonth );
assertNull( theEntity.vmCreatedSqlZonedDateTime );
assertNull( theEntity.name );
s.save( theEntity );
//TODO: Actually the values should be non-null afterQuery save
@ -68,6 +89,15 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
assertNull( theEntity.vmCreatedSqlDate );
assertNull( theEntity.vmCreatedSqlTime );
assertNull( theEntity.vmCreatedSqlTimestamp );
assertNull( theEntity.vmCreatedSqlLocalDate );
assertNull( theEntity.vmCreatedSqlLocalTime );
assertNull( theEntity.vmCreatedSqlLocalDateTime );
assertNull( theEntity.vmCreatedSqlMonthDay );
assertNull( theEntity.vmCreatedSqlOffsetDateTime );
assertNull( theEntity.vmCreatedSqlOffsetTime );
assertNull( theEntity.vmCreatedSqlYear );
assertNull( theEntity.vmCreatedSqlYearMonth );
assertNull( theEntity.vmCreatedSqlZonedDateTime );
assertNull( theEntity.name );
s.getTransaction().commit();
s.close();
@ -85,6 +115,15 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
assertNotNull( theEntity.vmCreatedSqlDate );
assertNotNull( theEntity.vmCreatedSqlTime );
assertNotNull( theEntity.vmCreatedSqlTimestamp );
assertNotNull( theEntity.vmCreatedSqlLocalDate );
assertNotNull( theEntity.vmCreatedSqlLocalTime );
assertNotNull( theEntity.vmCreatedSqlLocalDateTime );
assertNotNull( theEntity.vmCreatedSqlMonthDay );
assertNotNull( theEntity.vmCreatedSqlOffsetDateTime );
assertNotNull( theEntity.vmCreatedSqlOffsetTime );
assertNotNull( theEntity.vmCreatedSqlYear );
assertNotNull( theEntity.vmCreatedSqlYearMonth );
assertNotNull( theEntity.vmCreatedSqlZonedDateTime );
assertEquals( "Bob", theEntity.name );
theEntity.lastName = "Smith";
@ -173,6 +212,36 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
@CreationTimestamp
private Timestamp vmCreatedSqlTimestamp;
@CreationTimestamp
private Instant vmCreatedSqlInstant;
@CreationTimestamp
private LocalDate vmCreatedSqlLocalDate;
@CreationTimestamp
private LocalTime vmCreatedSqlLocalTime;
@CreationTimestamp
private LocalDateTime vmCreatedSqlLocalDateTime;
@CreationTimestamp
private MonthDay vmCreatedSqlMonthDay;
@CreationTimestamp
private OffsetDateTime vmCreatedSqlOffsetDateTime;
@CreationTimestamp
private OffsetTime vmCreatedSqlOffsetTime;
@CreationTimestamp
private Year vmCreatedSqlYear;
@CreationTimestamp
private YearMonth vmCreatedSqlYearMonth;
@CreationTimestamp
private ZonedDateTime vmCreatedSqlZonedDateTime;
@UpdateTimestamp
private Timestamp updated;