JPA - saving changes without persist() invoked

后端 未结 1 1205
离开以前
离开以前 2020-12-15 10:39

We are using Toplink implementation of JPA + Spring + EJB. In one of our EJBs we have something like this:

public void updateUser(long userId, String newName         


        
1条回答
  •  遥遥无期
    2020-12-15 11:06

    You're working with a managed entity. If the entity does not become detached because its entity manager is closed, all changes done to the entity are reflected to the database when the session is flushed/closed and the transaction commited.

    From the Java EE tutorial:

    The state of persistent entities is synchronized to the database when the transaction with which the entity is associated commits.

    Edit for clarity and explanation: So there are three distinct modes that an entity could be in during its lifecycle:

    • Unsaved: The entity has been instantiated, but persist() has not been called yet.
    • Managed: The entity has been persisted using persist(), or loaded from the database, and is associated with an entity manager session. All changes to the entity are reflected to the database when the entity manager session is flushed.
    • Detached: The entity's entity manager session was closed. Changes to the entity will not be reflected to the database automatically, but can be merged explicitly using the merge() command.

    0 讨论(0)
提交回复
热议问题