HHH-2907 Adding @CreationTimestamp generator annotation
This commit is contained in:
parent
fd57a751b4
commit
fb0d0c5cf7
|
@ -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.
|
||||||
|
* <p>
|
||||||
|
* Supported property types:
|
||||||
|
* <ul>
|
||||||
|
* <li>{@link java.util.Date}</li>
|
||||||
|
* <li>{@link java.util.Calendar}</li>
|
||||||
|
* <li>{@link java.sql.Date}</li>
|
||||||
|
* <li>{@link java.sql.Time}</li>
|
||||||
|
* <li>{@link java.sql.Timestamp}</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @author Gunnar Morling
|
||||||
|
*/
|
||||||
|
@ValueGenerationType(generatedBy = CreationTimestampGeneration.class)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface CreationTimestamp {
|
||||||
|
}
|
|
@ -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<CreationTimestamp> {
|
||||||
|
|
||||||
|
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<Date> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Date generateValue(Session session, Object owner) {
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CreationTimestampGeneratorForCalendar implements ValueGenerator<Calendar> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Calendar generateValue(Session session, Object owner) {
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime( new Date() );
|
||||||
|
return calendar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CreationTimestampGeneratorForSqlDate implements ValueGenerator<java.sql.Date> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public java.sql.Date generateValue(Session session, Object owner) {
|
||||||
|
return new java.sql.Date( new Date().getTime() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CreationTimestampGeneratorForSqlTime implements ValueGenerator<Time> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Time generateValue(Session session, Object owner) {
|
||||||
|
return new Time( new Date().getTime() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CreationTimestampGeneratorForSqlTimestamp implements ValueGenerator<Timestamp> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Timestamp generateValue(Session session, Object owner) {
|
||||||
|
return new Timestamp( new Date().getTime() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,27 +23,30 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.generated;
|
package org.hibernate.test.generated;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import java.sql.Time;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.annotations.ColumnDefault;
|
import org.hibernate.annotations.ColumnDefault;
|
||||||
|
import org.hibernate.annotations.CreationTimestamp;
|
||||||
import org.hibernate.annotations.Generated;
|
import org.hibernate.annotations.Generated;
|
||||||
import org.hibernate.annotations.GenerationTime;
|
import org.hibernate.annotations.GenerationTime;
|
||||||
import org.hibernate.annotations.GeneratorType;
|
import org.hibernate.annotations.GeneratorType;
|
||||||
import org.hibernate.tuple.ValueGenerator;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
import org.hibernate.tuple.ValueGenerator;
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for the generation of column values using different
|
* Test for the generation of column values using different
|
||||||
|
@ -61,9 +64,18 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
|
||||||
s.beginTransaction();
|
s.beginTransaction();
|
||||||
TheEntity theEntity = new TheEntity( 1 );
|
TheEntity theEntity = new TheEntity( 1 );
|
||||||
assertNull( theEntity.createdDate );
|
assertNull( theEntity.createdDate );
|
||||||
|
assertNull( theEntity.vmCreatedDate );
|
||||||
|
assertNull( theEntity.vmCreatedSqlDate );
|
||||||
|
assertNull( theEntity.vmCreatedSqlTime );
|
||||||
|
assertNull( theEntity.vmCreatedSqlTimestamp );
|
||||||
assertNull( theEntity.name );
|
assertNull( theEntity.name );
|
||||||
s.save( theEntity );
|
s.save( theEntity );
|
||||||
|
//TODO: Actually the values should be non-null after save
|
||||||
assertNull( theEntity.createdDate );
|
assertNull( theEntity.createdDate );
|
||||||
|
assertNull( theEntity.vmCreatedDate );
|
||||||
|
assertNull( theEntity.vmCreatedSqlDate );
|
||||||
|
assertNull( theEntity.vmCreatedSqlTime );
|
||||||
|
assertNull( theEntity.vmCreatedSqlTimestamp );
|
||||||
assertNull( theEntity.name );
|
assertNull( theEntity.name );
|
||||||
s.getTransaction().commit();
|
s.getTransaction().commit();
|
||||||
s.close();
|
s.close();
|
||||||
|
@ -75,7 +87,12 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
|
||||||
s.beginTransaction();
|
s.beginTransaction();
|
||||||
theEntity = (TheEntity) session.get( TheEntity.class, 1 );
|
theEntity = (TheEntity) session.get( TheEntity.class, 1 );
|
||||||
assertNotNull( theEntity.createdDate );
|
assertNotNull( theEntity.createdDate );
|
||||||
|
assertNotNull( theEntity.vmCreatedDate );
|
||||||
|
assertNotNull( theEntity.vmCreatedSqlDate );
|
||||||
|
assertNotNull( theEntity.vmCreatedSqlTime );
|
||||||
|
assertNotNull( theEntity.vmCreatedSqlTimestamp );
|
||||||
assertEquals( "Bob", theEntity.name );
|
assertEquals( "Bob", theEntity.name );
|
||||||
|
|
||||||
s.delete( theEntity );
|
s.delete( theEntity );
|
||||||
s.getTransaction().commit();
|
s.getTransaction().commit();
|
||||||
s.close();
|
s.close();
|
||||||
|
@ -97,6 +114,22 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
|
||||||
@Column( nullable = false )
|
@Column( nullable = false )
|
||||||
private Date createdDate;
|
private Date createdDate;
|
||||||
|
|
||||||
|
@CreationTimestamp
|
||||||
|
private Date vmCreatedDate;
|
||||||
|
|
||||||
|
@CreationTimestamp
|
||||||
|
private Calendar vmCreatedCalendar;
|
||||||
|
|
||||||
|
@CreationTimestamp
|
||||||
|
private java.sql.Date vmCreatedSqlDate;
|
||||||
|
|
||||||
|
@CreationTimestamp
|
||||||
|
private Time vmCreatedSqlTime;
|
||||||
|
|
||||||
|
@CreationTimestamp
|
||||||
|
private Timestamp vmCreatedSqlTimestamp;
|
||||||
|
|
||||||
|
|
||||||
@GeneratorType( type = MyVmValueGenerator.class, when = GenerationTime.INSERT )
|
@GeneratorType( type = MyVmValueGenerator.class, when = GenerationTime.INSERT )
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue