Friday, February 22, 2013

EntityManager

http://www.objectdb.com/java/jpa/persistence/managed


Table above summarizes the methods of the EntityManager class to manage individual entities.
Method
ReturnsDescription
persist(Object entity)Make entity instance persistent
merge(T entity)Merge the state of the entity
refresh(Object entity)Refresh the in-memory state of the instance
remove(Object entity)Remove the entity instance
contains(Object entity)booleanCheck if the instance exists
find(Class<T> ty, Object key)TFind by primary key
getReference(Class<T> type, Object key)TGet lazily fetched instance (proxy)


flush()

Summary of EntityManager methods to manage individual entities.

Working with JPA Entity Objects

Entity objects are in-memory instances of entity classes (persistable user defined classes), which can represent physical objects in the database.
Managing an ObjectDB Object Database using JPA requires using entity objects for many operations, including storing, retrieving, updating and deleting database objects.
This page covers the following topics:

Entity Object Life Cycle

The life cycle of entity objects consists of four states: New, Managed, Removed and Detached.

------------
The Persistence Context (Managed Entities)

Managed Entity      flush()/commit() ->    DB
Managed Entity      <- refresh()   DB

------------
When an entity object is initially created its state is New.  In this state the object is not yet associated with an EntityManager and has no representation in the database.
An entity object becomes Managed when it is persisted to the database via an EntityManager’s persist method, which must be invoked within an active transaction. On transaction commit, the owning EntityManager stores the new entity object to the database. More details on storing objects are provided in the Storing Entities section.
Entity objects retrieved from the database by an EntityManager are also in the Managedstate. Object retrieval is discussed in more detail in the Retrieving Entities section.
If a managed entity object is modified within an active transaction the change is detected by the owning EntityManager and the update is propagated to the database on transaction commit.  See the Updating Entities section for more information about making changes to entities.
A managed entity object can also be retrieved from the database and marked for deletion, by using the EntityManager’s remove method within an active transaction. The entity object changes its state from Managed to Removed, and is physically deleted from the database during commit. More details on object deletion are provided in the Deleting Entities section.
The last state, Detached, represents entity objects that have been disconnected from the EntityManager. For instance, all the managed objects of an EntityManager become detached when the EntityManager is closed. Working with detached objects, including merging them back to an EntityManager, is discussed in the Detached Entities section.

The Persistence Context

The persistence context is the collection of all the managed objects of an EntityManager. If an entity object that has to be retrieved already exists in the persistence context, the existing managed entity object is returned without actually accessing the database (except retrieval by refresh, which always requires accessing the database).
The main role of the persistence context is to make sure that a database entity object is represented by no more than one in-memory entity object within the same EntityManager. EveryEntityManager manages its own persistence context. Therefore, a database object can be represented by different memory entity objects in different EntityManager instances. But retrieving the same database object more than once using the same EntityManager should always result in the same in-memory entity object.
Another way of looking at it is that the persistence context also functions as a local cache for a given EntityManager. ObjectDB also manages a level 2 shared cache for the EntityManagerFactory as well as other caches as explained in the Configuration chapter.
By default, managed entity objects that have not been modified or removed during a transaction are held in the persistence context by weak references. Therefore, when a managed entity object is no longer in use by the application the garbage collector can discard it and it is automatically removed from the persistence context. ObjectDB can be configured to use strong references or soft references instead of weak references.
The contains method can check if a specified entity object is in the persistence context:
    boolean isManaged = em.contains(employee);
The persistence context can be cleared by using the clear method, as so:
    em.clear();
When the persistence context is cleared all of its managed entities become detached and any changes to entity objects that have not been flushed to the database are discarded. Detached entity objects are discussed in more detail in the Detached Entities section.


How to get primary key of newly inserted object


https://forum.hibernate.org/viewtopic.php?p=2351114

You should be just fine with your EntityManager. It has different naming but the same meaning. Once you invoke entityManager.persist(someInstance), you should be able to get someInstance's id by calling someInstance.getId() 

entityManager.persist(transientInstance);
entityManager.flush();
log.debug("persist successful:" + transientInstance.getId());





More Reading:
http://www.jpalace.org/docs/tutorials/jee/jpa_6.html

http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#flush()

No comments:

Post a Comment