I have an java.persistence.Entity class, that I instantiate and persist using java.persistence.EntityManager.persist. For safety I make sure it's merged into the persistence context and I'm good.
Now, I know that when I create the entity from scratch, I need to manually persist it via the EntityManager, but what about if I simply retrieved an already existing record as an entity? My testing indicates that simply calling setters on that entity results in the updates making it to the database, but I'm looking for something definitive to rely on. So please can anyone shed light or a source on when (reliably) do updates to an attached entity get written disk. At this point, I've no choice but to manually persist the entity after making my changes but I rather know when and when I don't have to.
Update:(thanks Yusuf) according to the accepted answer mentioned below, my question is answered. JPA EntityManager: Why use persist() over merge()?.
According to that answer persist itself does nothing other than add the entity to the persistence context. And merge adds a new object to the context. The implication then is that persistence is simply a function of being part of the persistence context, and since by getting hold of an entity via the entity manager, the entity is attached to the context, so it follows that there is no need at all to call persist.