persist vs merge code example
Example: entitymanager.persist
JPA uses the EntityManager API for runtime usage.
The EntityManager represents the application session or
dialog with the database.
Each request, or each client will
use its own EntityManager to access the database.
The EntityManager also represents a transaction context,
and in a typical stateless model
a new EntityManager is created for each transaction.
In a stateful model, an EntityManager may match the
lifecycle of a client's session.
The EntityManager provides an API for all required
persistence operations.
These include the following CRUD operations:
persist (INSERT)
merge (UPDATE)
remove (DELETE)
find (SELECT)
The EntityManager is an object-oriented API,
so does not map directly onto database SQL or DML operations.
For example to update an object,
you just need to read the object and change
its state through its set methods,
and then call commit on the transaction.
The EntityManager figures out which objects
you changed and performs the correct updates to the database,
there is no explicit update operation in JPA.