Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-2907 Adding @UpdateTimestamp generator annotation
- Loading branch information
1 parent
fb0d0c5
commit 1e39c4b
Showing
5 changed files
with
268 additions
and
49 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
hibernate-core/src/main/java/org/hibernate/annotations/UpdateTimestamp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
hibernate-core/src/main/java/org/hibernate/tuple/TimestampGenerators.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() ); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
hibernate-core/src/main/java/org/hibernate/tuple/UpdateTimestampGeneration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters