我应该手动提交这个 Play!交易?
了解如何手动提交 Play 之后! JPA 事务 ...我不确定我是否真的需要。
我有两个控制器操作:一个添加网站,然后立即重定向到下一个...显示其编辑表单。
public static void added(String title){
Task task= new Task();
website.title = title;
task.save();
// Do I really need to commit this transaction here?
// Note that task.id is already filled here, somehow
// https://stackoverflow.com/questions/8169640/how-does-an-entity-get-an-id-before-a-transaction-is-committed-in-jpa-play
JPA.em().getTransaction().commit();
edit(task.id);
}
public static void edit(long taskId) {
Task task = Task.find("byId", taskId).first();
render(task);
}
在重定向到 edit()
之前是否需要提交事务?
After finding out how to manually commit a Play! JPA transaction ... I'm not sure I really need to.
I have two controller actions: one that adds a website, and immediately redirects to the next one ... which shows its edit form.
public static void added(String title){
Task task= new Task();
website.title = title;
task.save();
// Do I really need to commit this transaction here?
// Note that task.id is already filled here, somehow
// https://stackoverflow.com/questions/8169640/how-does-an-entity-get-an-id-before-a-transaction-is-committed-in-jpa-play
JPA.em().getTransaction().commit();
edit(task.id);
}
public static void edit(long taskId) {
Task task = Task.find("byId", taskId).first();
render(task);
}
Is there a need to commit the transaction before redirecting to edit()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。:)(虽然将来可能会出现您需要控制事务处理的情况,但这似乎不是其中之一;正如您还发现的那样,Play!在保存后立即刷新会话,因此您可以访问自动生成的 PK ID。因为这似乎是您尝试此操作的唯一原因,所以我会让 Play! 执行其操作,并且仅在您确实需要时才劫持控制权。)
No. :) (Though there may be situations in the future where you may need to take control of the transaction handling, this does not appear to be one of them; as you also discovered, Play! flushes the session immediately after saving, so you have access to the auto-generated PK ID. As this appears to be the only reason you were attempting this, I would let Play! do its thing and only hijack control if/when you really need to.)