View Javadoc
1   package ejava.examples.daoex.dao;
2   
3   import java.util.List;
4   
5   import javax.persistence.EntityManager;
6   
7   import ejava.examples.daoex.bo.Book;
8   
9   /**
10   * This class provides a simple DAO implementation based on JPA
11   */
12  public class JPABookDAOImpl implements BookDAO {
13  	private EntityManager em;
14  	
15  	public void setEntityManager(EntityManager em) {
16  		this.em = em;
17  	}
18  
19  	@Override
20  	public Book create(Book book) {
21  		em.persist(book);
22  		return book;
23  	}
24  
25  	@Override
26  	public Book update(Book book) {
27  		return em.merge(book);
28  	}
29  
30  	@Override
31  	public Book get(long id) {
32  		return em.find(Book.class, id);
33  	}
34  
35  	@Override
36  	public void remove(Book book) {
37  		em.remove(book);
38  	}
39  
40  	@Override
41  	public List<Book> findAll(int offset, int limit) {
42  		return em.createQuery("select b from Book b", Book.class)
43  				.setFirstResult(offset)
44  				.setMaxResults(limit)
45  				.getResultList();
46  	}
47  
48  }