Java Reference
In-Depth Information
that an entity be updated, it tracks down the relational data that corresponds to the entity
and updates it. Likewise, the EntityManager removes the relational data when you re-
quest that an entity be deleted. From the other side of the translation bridge, when you re-
quest that an entity be “found” in the database, the EntityManager creates the Entity
object, populates it with relational data, and “returns” it to the OO world.
Figure 10.1. The EntityManager acts as a bridge between the OO and relational worlds. It interprets the O/R
mapping specified for an entity and saves the entity in the database.
Besides providing these explicit SQL-like CRUD operations, the EntityManager also
quietly tries to keep entities synched with the database automatically as long as they're
within the EntityManager's reach (this behind-the-scenes synchronization is what we mean
when we talk about “managed” entities in the next section). The EntityManager is eas-
ily the most important interface in JPA and is responsible for most of the O/R (object-rela-
tional) mapping magic in the API.
Despite all this under-the-hood power, the EntityManager is a small, simple, and in-
tuitive interface, especially compared to the mapping steps we discussed in the previous
chapter and the query API, which we'll explore in the next chapter. Once we go over some
basic concepts in the next few sections, the interface will seem almost trivial. You might
already agree if you take a quick look at table 10.1 . It lists some of the most commonly
used methods defined in the EntityManager interface.
Table 10.1. EntityManager is used to perform CRUD operations. Here are the most commonly used methods of
the EntityManager interface.
Method signature
Description
Saves (persists) an entity into the database and makes the
entity managed.
public void persist(Object entity);
Merges an entity to the EntityManager's persistence con-
text and returns the merged entity.
public <T> T merge(T entity);
public void remove(Object entity);
Removes an entity from the database.
 
 
Search WWH ::




Custom Search