为什么我的更新不起作用?
我是 GAE 和 JDO 的新手,我对如何更新数据感到困惑。
使用下面的代码,如果我对一个对象执行 getAll()
,然后执行 get()
,然后更改 get() 返回的该对象的属性,然后getAll()
,第二次调用 getAll()
返回原始的未更改的对象。
我尝试执行flush()但这似乎没有帮助。如果我重新启动码头,数据不会保留。
public class Notes {
@SuppressWarnings("unchecked")
public List<Note> getAll() {
PersistenceManager pm = PMF.instance().getPersistenceManager();
Query query = pm.newQuery("select from com.uptecs.google1.model.Note order by subject");
return (List<Note>) query.execute();
}
public void add(Note note) {
PersistenceManager pm = PMF.instance().getPersistenceManager();
pm.makePersistent(note);
pm.flush();
}
public Note get(long id) {
PersistenceManager pm = PMF.instance().getPersistenceManager();
return (Note)pm.getObjectById(Note.class, id);
}
public void update(Note note) {
PersistenceManager pm = PMF.instance().getPersistenceManager();
pm.flush();
}
}
I am new to GAE, and to JDO, I am getting stuck with how to update data.
Using the code below, if I do a getAll()
, then a get()
on an object, then alter an attribute for that object returned by get(), then a getAll()
, the second call to getAll()
returns the original un-altered object.
I tried doing a flush() but that doesn't seem to help. If I restart jetty, the data is not persisted.
public class Notes {
@SuppressWarnings("unchecked")
public List<Note> getAll() {
PersistenceManager pm = PMF.instance().getPersistenceManager();
Query query = pm.newQuery("select from com.uptecs.google1.model.Note order by subject");
return (List<Note>) query.execute();
}
public void add(Note note) {
PersistenceManager pm = PMF.instance().getPersistenceManager();
pm.makePersistent(note);
pm.flush();
}
public Note get(long id) {
PersistenceManager pm = PMF.instance().getPersistenceManager();
return (Note)pm.getObjectById(Note.class, id);
}
public void update(Note note) {
PersistenceManager pm = PMF.instance().getPersistenceManager();
pm.flush();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要获得良好的概述,请参阅以下文章:
http://www.ibm.com/developerworks/java/library/j -gaej1/
http://www.ibm.com/developerworks/java/库/j-gaej2/index.html
http://www.ibm.com/developerworks/java/library/ j-gaej3.html
2 和 3 最相关。
For a good overview look at these articles:
http://www.ibm.com/developerworks/java/library/j-gaej1/
http://www.ibm.com/developerworks/java/library/j-gaej2/index.html
http://www.ibm.com/developerworks/java/library/j-gaej3.html
2 and 3 are most relevant.
您是否看过 AppEngine 入门指南?他们提供了关于使用 JDO API 的非常全面的指南。
听起来您没有修改后调用 close() 持久对象。
Have you looked at the AppEngine Getting Started Guide? They have a pretty extensive guide on using the JDO API.
Sounds like you aren't calling close() after modifying the persistent object.
我对 JDO 不太熟悉,但是在刷新之前不需要 commit() 或 save() 数据吗?我认为只有这些语句才会保留在数据库中。
I am not very familiar with JDO but don't you have to commit() or save() your data before flush? I think only these statements will persist in the database.
也许实际关闭你的 PersistenceManagers 可能会有所帮助(不用介意内存利用率的原因!)
Perhaps actually closing your PersistenceManagers may help (never mind the memory utilisation reasons!)