我正在使用Google App Engine,并使用有关JDO的Google文档创建了一个持久性实体。该课程如下:
@PersistenceCapable
public class Message {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
public long id;
@Persistent
public Text message;
@Persistent
public boolean isNew;
@Persistent
public long categoryId;
@Persistent
public boolean plus;
@Persistent
public Date lastUpdate;
Message(long id, String message, boolean isNew, long categoryId, Date lastUpdate, boolean plus) {
this.id = id;
this.message = new Text(message);
this.isNew = isNew;
this.categoryId = categoryId;
this.lastUpdate = lastUpdate;
this.plus = plus;
}
}
然后,我使用以下doPost代码创建一个HttpServlet:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/plain");
PrintWriter writer = resp.getWriter();
Date tenDaysAgo = new Date(new Date().getTime()-TEN_DAYS_IN_MILISSECOND);
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
Query queryMessages = pm.newQuery(Message.class);
queryMessages.setFilter("isNew == True && lastUpdate <= lastUpdateParam");
queryMessages.declareParameters(Date.class.getName() + " lastUpdateParam");
List<Message> results = (List<Message>) queryMessages.execute(tenDaysAgo);
for(Message msg : results) {
msg.isNew = false;
pm.makePersistent(msg);
}
//pm.makePersistentAll(results);
writer.print(results.size() + " messages changed.");
}finally {
pm.close();
}
}
但是,当我发布帖子请求时,我收到消息“3048条消息已更改”。我检查数据库,数据没有变化。持久性不能保持我在对象中所做的更改。即使使用makePersistentAll(list)或makePersistent(object),结果也是一样的:数据库中没有变化。
我缺少什么?
谢谢!
答案 0 :(得分:0)
您需要创建一个JDO事务来跟踪更改,然后提交它们以发出更新SQL语句:
pm.currentTransaction().begin();
for(Message msg : results) {
msg.isNew = false;
}
pm.currentTransaction().commit();