[英]How to store created/lastUpdate fields in AppEngine DataStore using Java JDO 3?
I have a working application in Appengine using Java and JDO 3. I found these arguments ( auto_now and auto_now_add ) which correspond exactly what I want to implement in Java. 我在使用Java和JDO 3的Appengine中有一个正在运行的应用程序。我发现这些参数( auto_now和auto_now_add )与我要在Java中实现的确切对应。 So essentially the question is: How to convert AppEngine's Python DateTimeProperty to Java JDO?
因此,本质上的问题是:如何将AppEngine的Python DateTimeProperty转换为Java JDO?
@PersistenceCapable public class MyEntity {
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY, primaryKey = "true")
private Long id;
@Persistent private String name;
...
@Persistent public void getLastUpdate() { return new Date(); }
@Persistent public void getLastUpdate() { return new Date(); }
@Persistent public void getLastUpdate() { return new Date(); }
As suggested by answer, but it seems to always update the value, even when I just load the value from the datastore or just modify an unrelated field (eg String name
). 如答案所示,但即使我只是从数据存储区中加载值或只是修改不相关的字段(例如
String name
),它似乎总是会更新该值。
You can easily enough have a property (setter/getter) on a java class and have the property persistable (rather than the field).
您可以轻松地在java类上具有一个属性(setter / getter),并使该属性可持久(而不是字段)。 Within that getter you can code whatever you want to control what value goes into the datastore.
在该getter中,您可以编写任何代码,以控制将什么值放入数据存储区。
If I didn't do the following hack, I can't read the value stored in the datastore [neither with the hack :( ]: 如果未进行以下修改,则无法读取存储在数据存储区中的值[hack :(]均不可用:
@Persistent public Date getLastUpdate() { return new Date(); }
private Date prevUpdate;
public void setLastUpdate(Date lastUpdate) { this.prevUpdate = lastUpdate; }
public Date getPrevUpdate() { return prevUpdate; }
Is there any way to differentiate if a persistence operation is in progress or my code is calling the getter? 有什么方法可以区分是正在进行持久性操作还是代码正在调用getter?
@Persistent(customValueStrategy = "auto_now_add") private Date lastUpdate;
@Persistent(customValueStrategy = "auto_now_add") private Date lastUpdate;
I modeled auto_now_add
after org.datanucleus.store.valuegenerator.TimestampGenerator
replacing Timestamp
with java.util.Date
. 在
org.datanucleus.store.valuegenerator.TimestampGenerator
用java.util.Date
替换Timestamp
之后,我对auto_now_add
进行了建模。 But it was only populated once at the first makePersistent
call, regardless of how many times I modified other fields and called makePersistent
. 但是,无论我修改其他字段并调用
makePersistent
多少次,它在第一次makePersistent
调用中仅填充一次。 Also note that it doesn't seem to behave as the documentation says (or my English is rusty): 还要注意,它似乎不像文档中所说的那样工作(或者我的英语很生锈):
Please note that by defining a value-strategy for a field then it will, by default, always generate a value for that field on persist.
请注意,通过为字段定义值策略,默认情况下,它将在持久化时始终为该字段生成值。 If the field can store nulls and you only want it to generate the value at persist when it is null (ie you haven't assigned a value yourself) then you can add the extension "strategy-when-notnull" as false
如果该字段可以存储空值,并且您只希望它在为空值时(即您自己尚未分配值)在persist时生成值,则可以将扩展名“ strategy-when-notnull”添加为false
preStore
using PersistenceManager.addInstanceLifecycleListener
PersistenceManager.addInstanceLifecycleListener
preStore
Works as expected, but I could make it work across multiple entities using a base class. 可以按预期工作,但是我可以使用基类使其跨多个实体工作。
pm.addInstanceLifecycleListener(new StoreLifecycleListener() {
@Override public void preStore(InstanceLifecycleEvent event) {
MyEntity entity = (MyEntity)event.getPersistentInstance();
entity.setLastUpdate(new Date());
}
@Override public void postStore(InstanceLifecycleEvent event) {}
}, MyEntity.class);
implements StoreCallback
and public void jdoPreStore() { this.setLastUpdate(new Date()); }
implements StoreCallback
和public void jdoPreStore() { this.setLastUpdate(new Date()); }
public void jdoPreStore() { this.setLastUpdate(new Date()); }
Works as expected, but I could make it work across multiple entities using a base class. 可以按预期工作,但是我可以使用基类使其跨多个实体工作。
Whatever I do I can't make the following structure work: 无论我做什么,我都无法使以下结构起作用:
public abstract class Dateable implements StoreCallback {
@Persistent private Date created;
@Persistent private Date lastUpdate;
public Dateable() { created = new Date(); }
public void jdoPreStore() { this.setLastUpdate(new Date()); }
// ... normal get/set properties for the above two
}
@PersistenceCapable public class MyEntity extends Dateable {
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY, primaryKey = "true") private Long id;
@Persistent private String name;
The problems when the enhancer runs: 增强器运行时的问题:
public abstract class Dateable
: public abstract class Dateable
:
DataNucleus.MetaData Registering class "[...].Dateable" as not having MetaData.
DataNucleus.MetaData注册类“ [...]。Dateable”为没有MetaData。
public abstract class Dateable
with the above log, but running the code anyway: public abstract class Dateable
与上面的日志,但仍然运行代码:
Creation date changes whenever I create or read the data from datastore. 每当我从数据存储区创建或读取数据时,创建日期都会更改。
@PersistenceCapable public abstract class Dateable
: @PersistenceCapable public abstract class Dateable
:
DataNucleus.MetaData Class "[...].MyEntity" has been specified with 1 primary key fields, but this class is using datastore identity and should be application identity.
已经使用1个主键字段指定了DataNucleus.MetaData类“ [...]。MyEntity”,但是此类使用的是数据存储标识,应为应用程序标识。
JDO simply provides persistence of Java classes (and its fields/properties) so don't see what the design of JDO has to do with it. JDO只是提供Java类(及其字段/属性)的持久性,因此看不到JDO的设计与它有什么关系。
You can easily enough have a property (setter/getter) on a java class and have the property persistable (rather than the field). 您可以轻松地在java类上具有一个属性(setter / getter),并使该属性可持久(而不是字段)。 Within that getter you can code whatever you want to control what value goes into the datastore.
在该getter中,您可以编写任何代码,以控制将什么值放入数据存储区。 Either that or you use a preStore listener to be able to set things just before persistence so the desired value goes into the datastore.
要么,要么您使用preStore侦听器就可以在持久性之前进行设置,以便所需的值进入数据存储区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.