Java Reference
In-Depth Information
EntityTransaction to join an existing transaction. Unless circumstances demand it,
it's better to use the container-managed EntityManager because there are fewer manu-
al steps—and fewer chances to misuse the API.
Now that you have a basic handle on the EntityManager and EntityManager-
Factory , let's look at some of EntityManager 's persistence operations.
10.2. Persistence operations
The heart of the JPA API lies in the EntityManager operations, which we'll discuss in
upcoming sections. As you might have noted in listing 10.1 , although the EntityMan-
ager interface is small and simple, it's complete in its ability to provide an effective per-
sistence infrastructure. In addition to the CRUD functionality introduced in listing 10.1 ,
we'll cover a few less commonly used operations like flushing and refreshing.
Let's start our coverage in the most logical place: persisting new entities into the database.
10.2.1. Persisting entities
Recall that in listing 10.1 the addItem method persists an Item entity into the database.
Because listing 10.1 was quite a few pages back, we'll repeat the addItem method body
in the next listing. Although it isn't obvious, the code is especially helpful in understanding
how entity relationships are persisted, which we'll look at in greater detail in a minute. For
now, let's concentrate on the persist method itself.
Listing 10.3. Persisting entities
Search WWH ::




Custom Search