mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#555 - Use a common timestamp value for @WhenCreated and @WhenModified
This commit is contained in:
+4
-2
@@ -18,14 +18,16 @@ public class GeneratedCounter implements GeneratedProperty {
|
||||
/**
|
||||
* Always returns a 1.
|
||||
*/
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return BasicTypeConverter.convert(1, numberType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the current value by one.
|
||||
*/
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
Number currVal = (Number) prop.getValue(bean);
|
||||
Integer nextVal = currVal.intValue() + 1;
|
||||
return BasicTypeConverter.convert(nextVal, numberType);
|
||||
|
||||
+4
-2
@@ -15,14 +15,16 @@ public class GeneratedCounterInteger implements GeneratedProperty {
|
||||
/**
|
||||
* Always returns a 1.
|
||||
*/
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the current value by one.
|
||||
*/
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
Integer i = (Integer) prop.getValue(bean);
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
+4
-2
@@ -15,14 +15,16 @@ public class GeneratedCounterLong implements GeneratedProperty {
|
||||
/**
|
||||
* Always returns a 1.
|
||||
*/
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return (long) 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the current value by one.
|
||||
*/
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
Long i = (Long) prop.getValue(bean);
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
+5
-3
@@ -13,14 +13,16 @@ public class GeneratedInsertDate implements GeneratedProperty {
|
||||
/**
|
||||
* Return the current time as a Timestamp.
|
||||
*/
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return new Date(System.currentTimeMillis());
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new Date(now);
|
||||
}
|
||||
|
||||
/**
|
||||
* Just returns the beans original insert timestamp value.
|
||||
*/
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return prop.getValue(bean);
|
||||
}
|
||||
|
||||
|
||||
+7
-11
@@ -3,10 +3,6 @@ package com.avaje.ebeaninternal.server.deploy.generatedproperty;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
* Support java.time types as GeneratedProperty.
|
||||
*/
|
||||
@@ -35,7 +31,7 @@ public class GeneratedInsertJavaTime {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return prop.getValue(bean);
|
||||
}
|
||||
}
|
||||
@@ -46,8 +42,8 @@ public class GeneratedInsertJavaTime {
|
||||
public static class LocalDT extends Base {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return LocalDateTime.now();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return JavaTimeUtils.toLocalDateTime(now);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,8 +53,8 @@ public class GeneratedInsertJavaTime {
|
||||
public static class OffsetDT extends Base {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return OffsetDateTime.now();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return JavaTimeUtils.toOffsetDateTime(now);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -69,8 +65,8 @@ public class GeneratedInsertJavaTime {
|
||||
public static class ZonedDT extends Base {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return ZonedDateTime.now();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return JavaTimeUtils.toZonedDateTime(now);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-5
@@ -34,7 +34,7 @@ public class GeneratedInsertJodaTime {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return prop.getValue(bean);
|
||||
}
|
||||
}
|
||||
@@ -45,8 +45,8 @@ public class GeneratedInsertJodaTime {
|
||||
public static class LocalDT extends Base {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return new LocalDateTime();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new LocalDateTime(now);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,8 +56,8 @@ public class GeneratedInsertJodaTime {
|
||||
public static class DateTimeDT extends Base {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return new DateTime();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new DateTime(now);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-3
@@ -11,14 +11,16 @@ public class GeneratedInsertLong implements GeneratedProperty {
|
||||
/**
|
||||
* Return the current time as a Timestamp.
|
||||
*/
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return System.currentTimeMillis();
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return now;
|
||||
}
|
||||
|
||||
/**
|
||||
* Just returns the beans original insert timestamp value.
|
||||
*/
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return prop.getValue(bean);
|
||||
}
|
||||
|
||||
|
||||
+5
-3
@@ -13,14 +13,16 @@ public class GeneratedInsertTimestamp implements GeneratedProperty, GeneratedWhe
|
||||
/**
|
||||
* Return the current time as a Timestamp.
|
||||
*/
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return new Timestamp(System.currentTimeMillis());
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new Timestamp(now);
|
||||
}
|
||||
|
||||
/**
|
||||
* Just returns the beans original insert timestamp value.
|
||||
*/
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return prop.getValue(bean);
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -12,12 +12,12 @@ public interface GeneratedProperty {
|
||||
/**
|
||||
* Get the generated insert value for a specific property of a bean.
|
||||
*/
|
||||
Object getInsertValue(BeanProperty prop, EntityBean bean);
|
||||
Object getInsertValue(BeanProperty prop, EntityBean bean, long now);
|
||||
|
||||
/**
|
||||
* Get the generated update value for a specific property of a bean.
|
||||
*/
|
||||
Object getUpdateValue(BeanProperty prop, EntityBean bean);
|
||||
Object getUpdateValue(BeanProperty prop, EntityBean bean, long now);
|
||||
|
||||
/**
|
||||
* Return true if this should always be includes in an update statement.
|
||||
|
||||
+4
-4
@@ -14,15 +14,15 @@ public class GeneratedUpdateDate implements GeneratedProperty {
|
||||
/**
|
||||
* Return now as a Timestamp.
|
||||
*/
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return new Date(System.currentTimeMillis());
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new Date(now);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return now as a Timestamp.
|
||||
*/
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
return new Date(System.currentTimeMillis());
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new Date(now);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+12
-12
@@ -41,13 +41,13 @@ public class GeneratedUpdateJavaTime {
|
||||
public static class LocalDT extends Base {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return LocalDateTime.now();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return JavaTimeUtils.toLocalDateTime(now);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
return LocalDateTime.now();
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return JavaTimeUtils.toLocalDateTime(now);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,13 +57,13 @@ public class GeneratedUpdateJavaTime {
|
||||
public static class OffsetDT extends Base {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return OffsetDateTime.now();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return JavaTimeUtils.toOffsetDateTime(now);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
return OffsetDateTime.now();
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return JavaTimeUtils.toOffsetDateTime(now);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,13 +73,13 @@ public class GeneratedUpdateJavaTime {
|
||||
public static class ZonedDT extends Base {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return ZonedDateTime.now();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return JavaTimeUtils.toZonedDateTime(now);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
return ZonedDateTime.now();
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return JavaTimeUtils.toZonedDateTime(now);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-8
@@ -40,13 +40,13 @@ public class GeneratedUpdateJodaTime {
|
||||
public static class LocalDT extends Base {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return new LocalDateTime();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new LocalDateTime(now);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
return new LocalDateTime();
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new LocalDateTime(now);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,13 +56,13 @@ public class GeneratedUpdateJodaTime {
|
||||
public static class DateTimeDT extends Base {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return new DateTime();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new DateTime(now);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
return new DateTime();
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new DateTime(now);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -11,15 +11,15 @@ public class GeneratedUpdateLong implements GeneratedProperty {
|
||||
/**
|
||||
* Return now as a Timestamp.
|
||||
*/
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return System.currentTimeMillis();
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return now;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return now as a Timestamp.
|
||||
*/
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
return System.currentTimeMillis();
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return now;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+4
-4
@@ -13,15 +13,15 @@ public class GeneratedUpdateTimestamp implements GeneratedProperty, GeneratedWhe
|
||||
/**
|
||||
* Return now as a Timestamp.
|
||||
*/
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
return new Timestamp(System.currentTimeMillis());
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new Timestamp(now);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return now as a Timestamp.
|
||||
*/
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
return new Timestamp(System.currentTimeMillis());
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return new Timestamp(now);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-2
@@ -16,12 +16,12 @@ public class GeneratedWhoCreated implements GeneratedProperty {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return currentUserProvider.currentUser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -16,12 +16,12 @@ public class GeneratedWhoModified implements GeneratedProperty {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return currentUserProvider.currentUser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return currentUserProvider.currentUser();
|
||||
}
|
||||
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.avaje.ebeaninternal.server.deploy.generatedproperty;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
* Helper methods for Java time conversion.
|
||||
*/
|
||||
public class JavaTimeUtils {
|
||||
|
||||
/**
|
||||
* Return the system millis time as a LocalDateTime.
|
||||
*/
|
||||
public static Object toLocalDateTime(long systemMillis) {
|
||||
return new Timestamp(systemMillis).toLocalDateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the system millis time as a OffsetDateTime.
|
||||
*/
|
||||
public static Object toOffsetDateTime(long systemMillis) {
|
||||
return OffsetDateTime.ofInstant(Instant.ofEpochMilli(systemMillis), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the system millis time as a ZonedDateTime.
|
||||
*/
|
||||
public static Object toZonedDateTime(long systemMillis) {
|
||||
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(systemMillis), ZoneId.systemDefault());
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,8 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
|
||||
|
||||
protected final boolean logLevelSql;
|
||||
|
||||
protected final long now;
|
||||
|
||||
/**
|
||||
* The PreparedStatement used for the dml.
|
||||
*/
|
||||
@@ -49,6 +51,7 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
|
||||
protected ArrayList<UpdateGenValue> updateGenValues;
|
||||
|
||||
protected DmlHandler(PersistRequestBean<?> persistRequest, boolean emptyStringToNull) {
|
||||
this.now = System.currentTimeMillis();
|
||||
this.persistRequest = persistRequest;
|
||||
this.emptyStringToNull = emptyStringToNull;
|
||||
this.transaction = persistRequest.getTransaction();
|
||||
@@ -60,6 +63,11 @@ public abstract class DmlHandler implements PersistHandler, BindableRequest {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long now() {
|
||||
return now;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistRequestBean<?> getPersistRequest() {
|
||||
return persistRequest;
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ public class BindablePropertyInsertGenerated extends BindableProperty {
|
||||
|
||||
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
|
||||
|
||||
Object value = gen.getInsertValue(prop, bean);
|
||||
Object value = gen.getInsertValue(prop, bean, request.now());
|
||||
|
||||
// generated value should be the correct type
|
||||
if (bean != null) {
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ public class BindablePropertyUpdateGenerated extends BindableProperty {
|
||||
|
||||
public void dmlBind(BindableRequest request, EntityBean bean) throws SQLException {
|
||||
|
||||
Object value = gen.getUpdateValue(prop, bean);
|
||||
Object value = gen.getUpdateValue(prop, bean, request.now());
|
||||
|
||||
// generated value should be the correct type
|
||||
request.bind(value, prop);
|
||||
|
||||
@@ -56,4 +56,11 @@ public interface BindableRequest {
|
||||
PersistRequestBean<?> getPersistRequest();
|
||||
|
||||
void registerDerivedRelationship(DerivedRelationshipData assocBean);
|
||||
|
||||
/**
|
||||
* Return the system current time in millis. This is expected to the same time used
|
||||
* by multiple generated properties for a single request.
|
||||
*/
|
||||
long now();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user