How does an entity get an ID before a transaction is committed in JPA/Play?

后端 未结 4 655
春和景丽
春和景丽 2020-12-19 05:47

See this question.

It turns out that even without committing the transaction manually, before the TX is committed, the person has an ID after calling the save() meth

4条回答
  •  Happy的楠姐
    2020-12-19 06:24

    Play! is flushing the persistence context (which writes changes to the database and allows it to get the generated ID) every time you save an object using the save() method on the model:

    From the JPABase._save() source code:

    if (!em().contains(this)) {
        em().persist(this);
        PlayPlugin.postEvent("JPASupport.objectPersisted", this);
    }
    // ...
    try {
        em().flush();
    } catch (PersistenceException e) {
        // ...
    }
    

提交回复
热议问题