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()?
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.)