From 1085a8d6080bc1f50a02d210562f113d161c9c6a Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Fri, 24 Jul 2015 10:50:35 +1200 Subject: [PATCH] #348 - ENH: Add @WhenCreated and @WhenModified ... as alias to existing @CreatedTimestamp and @UpdatedTimstamp --- .../avaje/ebean/annotation/WhenCreated.java | 38 +++++++++++++++++++ .../avaje/ebean/annotation/WhenModified.java | 20 ++++++++++ .../deploy/meta/DeployBeanProperty.java | 6 ++- .../server/deploy/parse/AnnotationFields.java | 8 ++-- .../generated/TestGeneratedProperties.java | 2 + .../java/com/avaje/tests/model/BaseModel.java | 18 ++++----- .../java/com/avaje/tests/model/EGenProps.java | 24 ++++++++++++ .../avaje/tests/model/basic/BasicDomain.java | 8 ++-- .../com/avaje/tests/model/basic/Order.java | 4 +- .../TestOneToOneOptionalRelationship.java | 2 +- 10 files changed, 108 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/avaje/ebean/annotation/WhenCreated.java create mode 100644 src/main/java/com/avaje/ebean/annotation/WhenModified.java diff --git a/src/main/java/com/avaje/ebean/annotation/WhenCreated.java b/src/main/java/com/avaje/ebean/annotation/WhenCreated.java new file mode 100644 index 000000000..0d20fdc7a --- /dev/null +++ b/src/main/java/com/avaje/ebean/annotation/WhenCreated.java @@ -0,0 +1,38 @@ +package com.avaje.ebean.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * For a timestamp property that is set to the datetime when the entity is + * created/inserted. + *

+ * This is effectively an alias for @CreatedTimestamp and added to hint + * towards a better naming convention (WhenCreated, WhenModified). + *

+ *

+ * An alternative to using this annotation would be to use insertable=false, + * updateable=false with @Column and have the DB insert the current time + * (default value on the DB column is SYSTIME etc). + *

+ *

+ * The downside to this approach is that the inserted entity does not have the + * timestamp value after the insert has occurred. You need to fetch the entity + * back to get the inserted timestamp if you want to used it. + *

+ * + *

Example:

+ *
{@code
+ *
+ *   @WhenCreated
+ *   Timestamp whenCreated;
+ *
+ * }
+ */ +@Target({ ElementType.FIELD, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +public @interface WhenCreated { + +} diff --git a/src/main/java/com/avaje/ebean/annotation/WhenModified.java b/src/main/java/com/avaje/ebean/annotation/WhenModified.java new file mode 100644 index 000000000..0c4719183 --- /dev/null +++ b/src/main/java/com/avaje/ebean/annotation/WhenModified.java @@ -0,0 +1,20 @@ +package com.avaje.ebean.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * For a timestamp property that is set to the datetime when the entity was last modified. + *

+ * This is effectively an alias for @UpdatedTimestamp and added to hint + * towards a better naming convention (WhenCreated, WhenModified). + *

+ *

+ */ +@Target({ ElementType.FIELD, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +public @interface WhenModified { + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanProperty.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanProperty.java index cc741aa31..a19462738 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanProperty.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/meta/DeployBeanProperty.java @@ -13,6 +13,8 @@ import javax.persistence.Version; import com.avaje.ebean.annotation.CreatedTimestamp; import com.avaje.ebean.annotation.UpdatedTimestamp; +import com.avaje.ebean.annotation.WhenCreated; +import com.avaje.ebean.annotation.WhenModified; import com.avaje.ebean.config.ScalarTypeConverter; import com.avaje.ebean.config.dbplatform.DbEncrypt; import com.avaje.ebean.config.dbplatform.DbEncryptFunction; @@ -244,9 +246,9 @@ public class DeployBeanProperty { return ID_ORDER; } else if (undirectionalShadow) { return UNIDIRECTIONAL_ORDER; - } else if (field.getAnnotation(CreatedTimestamp.class) != null) { + } else if (field.getAnnotation(WhenCreated.class) != null || field.getAnnotation(CreatedTimestamp.class) != null) { return AUDITCOLUMN_ORDER; - } else if (field.getAnnotation(UpdatedTimestamp.class) != null) { + } else if (field.getAnnotation(WhenModified.class) != null || field.getAnnotation(UpdatedTimestamp.class) != null) { return AUDITCOLUMN_ORDER; } else if (field.getAnnotation(Version.class) != null) { return VERSIONCOLUMN_ORDER; diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java index d262bc19a..4bad6c26c 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/parse/AnnotationFields.java @@ -12,6 +12,8 @@ import com.avaje.ebean.annotation.HistoryExclude; import com.avaje.ebean.annotation.Index; import com.avaje.ebean.annotation.JsonIgnore; import com.avaje.ebean.annotation.UpdatedTimestamp; +import com.avaje.ebean.annotation.WhenCreated; +import com.avaje.ebean.annotation.WhenModified; import com.avaje.ebean.config.EncryptDeploy; import com.avaje.ebean.config.EncryptDeploy.Mode; import com.avaje.ebean.config.dbplatform.DbEncrypt; @@ -208,13 +210,11 @@ public class AnnotationFields extends AnnotationParser { prop.setFetchType(defaultLobFetchType); } - CreatedTimestamp ct = get(prop, CreatedTimestamp.class); - if (ct != null) { + if (get(prop, WhenCreated.class) != null || get(prop, CreatedTimestamp.class) != null) { generatedPropFactory.setInsertTimestamp(prop); } - UpdatedTimestamp ut = get(prop, UpdatedTimestamp.class); - if (ut != null) { + if (get(prop, WhenModified.class) != null || get(prop, UpdatedTimestamp.class) != null) { generatedPropFactory.setUpdateTimestamp(prop); } diff --git a/src/test/java/com/avaje/tests/generated/TestGeneratedProperties.java b/src/test/java/com/avaje/tests/generated/TestGeneratedProperties.java index a27858dd5..e571c80bc 100644 --- a/src/test/java/com/avaje/tests/generated/TestGeneratedProperties.java +++ b/src/test/java/com/avaje/tests/generated/TestGeneratedProperties.java @@ -18,6 +18,8 @@ public class TestGeneratedProperties extends BaseTestCase { assertNotNull(bean.getId()); assertNotNull(bean.getVersion()); + assertNotNull(bean.getWhenCreated()); + assertNotNull(bean.getWhenModified()); assertNotNull(bean.getTsCreated()); assertNotNull(bean.getTsUpdated()); assertNotNull(bean.getLdtCreated()); diff --git a/src/test/java/com/avaje/tests/model/BaseModel.java b/src/test/java/com/avaje/tests/model/BaseModel.java index 638cb3aa5..8f28a7686 100644 --- a/src/test/java/com/avaje/tests/model/BaseModel.java +++ b/src/test/java/com/avaje/tests/model/BaseModel.java @@ -7,8 +7,8 @@ import javax.persistence.MappedSuperclass; import javax.persistence.Version; import com.avaje.ebean.Model; -import com.avaje.ebean.annotation.CreatedTimestamp; -import com.avaje.ebean.annotation.UpdatedTimestamp; +import com.avaje.ebean.annotation.WhenCreated; +import com.avaje.ebean.annotation.WhenModified; @MappedSuperclass public class BaseModel extends Model { @@ -19,11 +19,11 @@ public class BaseModel extends Model { @Version Long version; - @CreatedTimestamp + @WhenCreated Timestamp whenCreated; - @UpdatedTimestamp - Timestamp whenUpdated; + @WhenModified + Timestamp whenModified; public Long getId() { return id; @@ -49,12 +49,12 @@ public class BaseModel extends Model { this.whenCreated = whenCreated; } - public Timestamp getWhenUpdated() { - return whenUpdated; + public Timestamp getWhenModified() { + return whenModified; } - public void setWhenUpdated(Timestamp whenUpdated) { - this.whenUpdated = whenUpdated; + public void setWhenModified(Timestamp whenModified) { + this.whenModified = whenModified; } } diff --git a/src/test/java/com/avaje/tests/model/EGenProps.java b/src/test/java/com/avaje/tests/model/EGenProps.java index 579af2754..f2b33b2a1 100644 --- a/src/test/java/com/avaje/tests/model/EGenProps.java +++ b/src/test/java/com/avaje/tests/model/EGenProps.java @@ -2,6 +2,8 @@ package com.avaje.tests.model; import com.avaje.ebean.annotation.CreatedTimestamp; import com.avaje.ebean.annotation.UpdatedTimestamp; +import com.avaje.ebean.annotation.WhenCreated; +import com.avaje.ebean.annotation.WhenModified; import javax.persistence.Entity; import javax.persistence.Id; @@ -22,6 +24,12 @@ public class EGenProps { @Version Long version; + @WhenCreated + Timestamp whenCreated; + + @WhenModified + Timestamp whenModified; + @CreatedTimestamp Timestamp tsCreated; @@ -76,6 +84,22 @@ public class EGenProps { this.version = version; } + public Timestamp getWhenCreated() { + return whenCreated; + } + + public void setWhenCreated(Timestamp whenCreated) { + this.whenCreated = whenCreated; + } + + public Timestamp getWhenModified() { + return whenModified; + } + + public void setWhenModified(Timestamp whenModified) { + this.whenModified = whenModified; + } + public Timestamp getTsCreated() { return tsCreated; } diff --git a/src/test/java/com/avaje/tests/model/basic/BasicDomain.java b/src/test/java/com/avaje/tests/model/basic/BasicDomain.java index 7de395bd7..7632fbcb3 100644 --- a/src/test/java/com/avaje/tests/model/basic/BasicDomain.java +++ b/src/test/java/com/avaje/tests/model/basic/BasicDomain.java @@ -7,8 +7,8 @@ import javax.persistence.Id; import javax.persistence.MappedSuperclass; import javax.persistence.Version; -import com.avaje.ebean.annotation.CreatedTimestamp; -import com.avaje.ebean.annotation.UpdatedTimestamp; +import com.avaje.ebean.annotation.WhenCreated; +import com.avaje.ebean.annotation.WhenModified; @MappedSuperclass public class BasicDomain implements Serializable { @@ -18,10 +18,10 @@ public class BasicDomain implements Serializable { @Id Integer id; - @CreatedTimestamp + @WhenCreated Timestamp cretime; - @UpdatedTimestamp + @WhenModified Timestamp updtime; @Version diff --git a/src/test/java/com/avaje/tests/model/basic/Order.java b/src/test/java/com/avaje/tests/model/basic/Order.java index 270137dee..feb1df336 100644 --- a/src/test/java/com/avaje/tests/model/basic/Order.java +++ b/src/test/java/com/avaje/tests/model/basic/Order.java @@ -22,8 +22,8 @@ import javax.persistence.Version; import javax.validation.constraints.NotNull; import javax.xml.bind.annotation.XmlType; -import com.avaje.ebean.annotation.CreatedTimestamp; import com.avaje.ebean.annotation.Formula; +import com.avaje.ebean.annotation.WhenCreated; import com.avaje.ebean.annotation.Where; /** @@ -90,7 +90,7 @@ public class Order implements Serializable { @Column(name = "name", table = "o_customer") String customerName; - @CreatedTimestamp + @WhenCreated Timestamp cretime; @Version diff --git a/src/test/java/com/avaje/tests/model/onetoone/TestOneToOneOptionalRelationship.java b/src/test/java/com/avaje/tests/model/onetoone/TestOneToOneOptionalRelationship.java index b96dd21d1..85a8b9653 100644 --- a/src/test/java/com/avaje/tests/model/onetoone/TestOneToOneOptionalRelationship.java +++ b/src/test/java/com/avaje/tests/model/onetoone/TestOneToOneOptionalRelationship.java @@ -69,7 +69,7 @@ public class TestOneToOneOptionalRelationship extends BaseTestCase { Assert.assertTrue(sql.contains(" from oto_account t0 left outer join oto_user t1 on t1.account_id = t0.id where t0.id = ?")); String lazyLoadSql = loggedSql.get(1); - Assert.assertTrue(lazyLoadSql.contains("select t0.id c0, t0.name c1, t0.version c2, t0.when_created c3, t0.when_updated c4, t0.account_id c5 from oto_user t0 where t0.id = ?")); + Assert.assertTrue(lazyLoadSql.contains("select t0.id c0, t0.name c1, t0.version c2, t0.when_created c3, t0.when_modified c4, t0.account_id c5 from oto_user t0 where t0.id = ?")); }