HHH-10406 - Provide @CreationTimestamp and @UpdateTimestamp for Java Time
This commit is contained in:
parent
fec5afc8b6
commit
8de4ff2a3a
|
@ -6,12 +6,6 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.tuple;
|
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;
|
import org.hibernate.annotations.CreationTimestamp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,24 +19,7 @@ public class CreationTimestampGeneration implements AnnotationValueGeneration<Cr
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(CreationTimestamp annotation, Class<?> propertyType) {
|
public void initialize(CreationTimestamp annotation, Class<?> propertyType) {
|
||||||
if ( java.sql.Date.class.isAssignableFrom( propertyType ) ) {
|
generator = TimestampGenerators.get(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" );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -8,57 +8,114 @@ package org.hibernate.tuple;
|
||||||
|
|
||||||
import java.sql.Time;
|
import java.sql.Time;
|
||||||
import java.sql.Timestamp;
|
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.Calendar;
|
||||||
import java.util.Date;
|
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.
|
* Generators for obtaining the current VM timestamp in different representations.
|
||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
*/
|
*/
|
||||||
/* package */ interface TimestampGenerators {
|
/* package */ final class TimestampGenerators {
|
||||||
|
|
||||||
class CurrentDateGenerator implements ValueGenerator<Date> {
|
private static final Map<Class<?>, ValueGenerator<?>> generators;
|
||||||
|
|
||||||
@Override
|
private TimestampGenerators() {
|
||||||
public Date generateValue(Session session, Object owner) {
|
|
||||||
return new Date();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
generators.put(
|
||||||
public Calendar generateValue(Session session, Object owner) {
|
Time.class,
|
||||||
Calendar calendar = Calendar.getInstance();
|
(session, owner) -> new Time( System.currentTimeMillis() )
|
||||||
calendar.setTime( new Date() );
|
);
|
||||||
return calendar;
|
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> {
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> ValueGenerator<T> get(final Class<T> type) {
|
||||||
@Override
|
final ValueGenerator<?> valueGeneratorSupplier = generators.get(
|
||||||
public java.sql.Date generateValue(Session session, Object owner) {
|
type );
|
||||||
return new java.sql.Date( new Date().getTime() );
|
if ( Objects.isNull( valueGeneratorSupplier ) ) {
|
||||||
|
throw new HibernateException(
|
||||||
|
"Unsupported property type [" + type.getName() + "] for @CreationTimestamp or @UpdateTimestamp generator annotation" );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class CurrentSqlTimeGenerator implements ValueGenerator<Time> {
|
return (ValueGenerator<T>) valueGeneratorSupplier;
|
||||||
|
|
||||||
@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() );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,6 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.tuple;
|
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;
|
import org.hibernate.annotations.UpdateTimestamp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,24 +19,7 @@ public class UpdateTimestampGeneration implements AnnotationValueGeneration<Upda
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(UpdateTimestamp annotation, Class<?> propertyType) {
|
public void initialize(UpdateTimestamp annotation, Class<?> propertyType) {
|
||||||
if ( java.sql.Date.class.isAssignableFrom( propertyType ) ) {
|
generator = TimestampGenerators.get(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" );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -8,6 +8,16 @@ package org.hibernate.test.generated;
|
||||||
|
|
||||||
import java.sql.Time;
|
import java.sql.Time;
|
||||||
import java.sql.Timestamp;
|
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.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
|
@ -59,6 +69,17 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
|
||||||
assertNull( theEntity.vmCreatedSqlDate );
|
assertNull( theEntity.vmCreatedSqlDate );
|
||||||
assertNull( theEntity.vmCreatedSqlTime );
|
assertNull( theEntity.vmCreatedSqlTime );
|
||||||
assertNull( theEntity.vmCreatedSqlTimestamp );
|
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 );
|
assertNull( theEntity.name );
|
||||||
s.save( theEntity );
|
s.save( theEntity );
|
||||||
//TODO: Actually the values should be non-null afterQuery save
|
//TODO: Actually the values should be non-null afterQuery save
|
||||||
|
@ -68,6 +89,15 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
|
||||||
assertNull( theEntity.vmCreatedSqlDate );
|
assertNull( theEntity.vmCreatedSqlDate );
|
||||||
assertNull( theEntity.vmCreatedSqlTime );
|
assertNull( theEntity.vmCreatedSqlTime );
|
||||||
assertNull( theEntity.vmCreatedSqlTimestamp );
|
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 );
|
assertNull( theEntity.name );
|
||||||
s.getTransaction().commit();
|
s.getTransaction().commit();
|
||||||
s.close();
|
s.close();
|
||||||
|
@ -85,6 +115,15 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
|
||||||
assertNotNull( theEntity.vmCreatedSqlDate );
|
assertNotNull( theEntity.vmCreatedSqlDate );
|
||||||
assertNotNull( theEntity.vmCreatedSqlTime );
|
assertNotNull( theEntity.vmCreatedSqlTime );
|
||||||
assertNotNull( theEntity.vmCreatedSqlTimestamp );
|
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 );
|
assertEquals( "Bob", theEntity.name );
|
||||||
|
|
||||||
theEntity.lastName = "Smith";
|
theEntity.lastName = "Smith";
|
||||||
|
@ -173,6 +212,36 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
|
||||||
@CreationTimestamp
|
@CreationTimestamp
|
||||||
private Timestamp vmCreatedSqlTimestamp;
|
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
|
@UpdateTimestamp
|
||||||
private Timestamp updated;
|
private Timestamp updated;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue