Java Reference
In-Depth Information
LISTING 12-3 (continued)
}
@Override
public void updateMovie(Movie movie) {
em.merge(movie);
}
@Override
public List<Movie> getAllMovies() {
return em.createQuery("SELECT m FROM Movie m", Movie.class)
.getResultList();
}
}
In Listing 12‐4, you create the DAO factory. The EntityManager is created and injected into this
class and then passed as a constructor argument to the createMovieDAO method that creates the
DAO object. The factory pattern is discussed in more detail in Chapter 6, so please refer to it for
more information.
LISTING 12‐4: The DAO Factory
package com.devchronicles.dataaccessobject;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@ApplicationScoped
public class MovieDAOFactory {
@PersistenceContext(unitName="moviePU")
private EntityManager em;
@Produces
public MovieDAO createMovieDAO() {
return new MovieDAOImpl(em);
}
}
The list of entities in your application is called a p ersistence unit, and the application's persistence
unit is dei ned in the persistence.xml coni guration i le. This i le should reside in your applica-
tion's META‐INF directory. The signii cant elements of persistence.xml follow:
Persistence unit name —You can give the persistence unit a name so that you can dei ne sev-
eral persistence units and then select them at run time.
Search WWH ::




Custom Search