我应该手动提交这个 Play!交易?

发布于 2024-12-16 16:57:28 字数 799 浏览 0 评论 0原文

了解如何手动提交 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就是爱搞怪 2024-12-23 16:57:28

不。:)(虽然将来可能会出现您需要控制事务处理的情况,但这似乎不是其中之一;正如您还发现的那样,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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文