View Javadoc
1   package ejava.examples.daoex.jpa;
2   
3   import javax.persistence.EntityManager;
4   import javax.persistence.TypedQuery;
5   
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   
9   import ejava.examples.daoex.bo.Author;
10  import ejava.examples.daoex.dao.AuthorDAO;
11  import ejava.examples.daoex.dao.DAOException;
12  
13  /**
14   * This class implements a DAO using javax.persistence.EntityManager. Most
15   * of the work of mapping the objects to the database is being performed in
16   * either the @Entity class or in a orm.xml descriptor file. The caller of 
17   * this object must manage the transaction scope. The EntityManager is being
18   * injected into the DAO at the start of the overall transaction.
19   */
20  public class JPAAuthorDAO implements AuthorDAO {
21      @SuppressWarnings("unused") 
22      private static final Logger logger = LoggerFactory.getLogger(JPAAuthorDAO.class);
23      private EntityManager em;
24      
25      /*
26       * This methos is not in the DAO interface and must be injected at a 
27       * point where we know the DAO's implementation class.
28       */
29      public void setEntityManager(EntityManager em) {
30          this.em = em;
31      }
32      
33      /* (non-Javadoc)
34       * @see ejava.examples.dao.jpa.AuthorDAO#create(ejava.examples.dao.domain.Author)
35       */
36      public void create(Author author) {
37          em.persist(author);
38      }
39      
40      /* (non-Javadoc)
41       * @see ejava.examples.dao.jpa.AuthorDAO#get(long)
42       */
43      public Author get(long id) {
44          return em.find(Author.class, id);
45      }
46      
47      /* (non-Javadoc)
48       * @see ejava.examples.dao.jpa.AuthorDAO#getByQuery(long)
49       */
50      public Author getByQuery(long id) {
51          TypedQuery<Author> query = em.createQuery("select a from jpaAuthor a where id=:id", Author.class)
52          		            .setParameter("id", id);
53          return query.getSingleResult();
54      }
55      
56      /* (non-Javadoc)
57       * @see ejava.examples.dao.jpa.AuthorDAO#update(ejava.examples.dao.domain.Author)
58       */
59      public Author update(Author author) throws DAOException {
60          Author dbAuthor = em.find(Author.class, author.getId());
61          if (dbAuthor!=null) {
62              dbAuthor.setFirstName(author.getFirstName());
63              dbAuthor.setLastName(author.getLastName());
64              dbAuthor.setSubject(author.getSubject());
65              dbAuthor.setPublishDate(author.getPublishDate());
66              return dbAuthor;
67          } else {
68              throw new DAOException("unable to locate author to update");
69          }
70      }
71  
72      /* (non-Javadoc)
73       * @see ejava.examples.dao.jpa.AuthorDAO#updateByMerge(ejava.examples.dao.domain.Author)
74       */
75      public Author updateByMerge(Author author) {
76          return em.merge(author);
77      }
78      
79      /* (non-Javadoc)
80       * @see ejava.examples.dao.jpa.AuthorDAO#remove(ejava.examples.dao.domain.Author)
81       */
82      public void remove(Author author) {
83          em.remove(author);
84      }
85  }