0

我正在使用 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) 结果是一样的:数据库没有变化。

我错过了什么?

谢谢!

4

1 回答 1

0

您需要创建一个 JDO 事务来跟踪更改,然后提交它们以发出更新 SQL 语句:

    pm.currentTransaction().begin();
    for(Message msg : results) {
        msg.isNew = false;
    }
    pm.currentTransaction().commit();
于 2013-07-11T21:28:35.067 回答