HHH-2907 Adding @UpdateTimestamp generator annotation

This commit is contained in:
Gunnar Morling 2013-11-06 12:18:21 +01:00 committed by Steve Ebersole
parent fb0d0c5cf7
commit 1e39c4b07f
5 changed files with 268 additions and 49 deletions

View File

@ -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.
* <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 = UpdateTimestampGeneration.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface UpdateTimestamp {
}

View File

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

View File

@ -0,0 +1,81 @@
/*
* 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.Session;
/**
* Generators for obtaining the current VM timestamp in different representations.
*
* @author Gunnar Morling
*/
/* package */ interface TimestampGenerators {
class CurrentDateGenerator implements ValueGenerator<Date> {
@Override
public Date generateValue(Session session, Object owner) {
return new Date();
}
}
class CurrentCalendarGenerator implements ValueGenerator<Calendar> {
@Override
public Calendar generateValue(Session session, Object owner) {
Calendar calendar = Calendar.getInstance();
calendar.setTime( new Date() );
return calendar;
}
}
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() );
}
}
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() );
}
}
}

View File

@ -0,0 +1,84 @@
/*
* 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.annotations.UpdateTimestamp;
/**
* Value generation implementation for {@link UpdateTimestamp}.
*
* @author Gunnar Morling
*/
public class UpdateTimestampGeneration implements AnnotationValueGeneration<UpdateTimestamp> {
private ValueGenerator<?> generator;
@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" );
}
}
@Override
public GenerationTiming getGenerationTiming() {
return GenerationTiming.ALWAYS;
}
@Override
public ValueGenerator<?> getValueGenerator() {
return generator;
}
@Override
public boolean referenceColumnInSql() {
return false;
}
@Override
public String getDatabaseGeneratedReferencedColumnValue() {
return null;
}
}

View File

@ -26,6 +26,7 @@ package org.hibernate.test.generated;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.sql.Time;
import java.sql.Timestamp;
@ -43,6 +44,7 @@ import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.GeneratorType;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.tuple.ValueGenerator;
@ -85,7 +87,7 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
s = openSession();
s.beginTransaction();
theEntity = (TheEntity) session.get( TheEntity.class, 1 );
theEntity = (TheEntity) s.get( TheEntity.class, 1 );
assertNotNull( theEntity.createdDate );
assertNotNull( theEntity.vmCreatedDate );
assertNotNull( theEntity.vmCreatedSqlDate );
@ -98,6 +100,47 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
s.close();
}
@Test
@TestForIssue(jiraKey = "HHH-2907")
public void testUpdateTimestampGeneration() {
Session s = openSession();
s.beginTransaction();
TheEntity theEntity = new TheEntity( 1 );
assertNull( theEntity.updated );
s.save( theEntity );
//TODO: Actually the value should be non-null after save
assertNull( theEntity.updated );
s.getTransaction().commit();
s.close();
Timestamp created = theEntity.vmCreatedSqlTimestamp;
Timestamp updated = theEntity.updated;
assertNotNull( updated );
assertNotNull( created );
s = openSession();
s.beginTransaction();
theEntity = (TheEntity) s.get( TheEntity.class, 1 );
theEntity.lastName = "Smith";
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
theEntity = (TheEntity) session.get( TheEntity.class, 1 );
assertEquals( "Creation timestamp should not change on update", created, theEntity.vmCreatedSqlTimestamp );
assertTrue( "Update timestamp should have changed due to update", theEntity.updated.after( updated ) );
s.delete( theEntity );
s.getTransaction().commit();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { TheEntity.class };
@ -129,10 +172,15 @@ public class DefaultGeneratedValueTest extends BaseCoreFunctionalTestCase {
@CreationTimestamp
private Timestamp vmCreatedSqlTimestamp;
@UpdateTimestamp
private Timestamp updated;
@GeneratorType( type = MyVmValueGenerator.class, when = GenerationTime.INSERT )
private String name;
@SuppressWarnings("unused")
private String lastName;
private TheEntity() {
}