1 package ejava.examples.cdiconfig.dao;
2
3 import java.util.List;
4
5 import javax.persistence.EntityManager;
6 import javax.persistence.PersistenceContext;
7 import javax.persistence.TypedQuery;
8
9 import ejava.examples.cdiconfig.CdiDemo;
10 import ejava.examples.cdiconfig.bo.Project;
11
12 public class ProjectsDaoImpl implements ProjectsDao {
13 private EntityManager em;
14
15 @PersistenceContext @CdiDemo
16 public void setEntityManager(EntityManager em) {
17 this.em = em;
18 }
19
20 @Override
21 public void create(Project task) {
22 em.persist(task);
23 }
24
25 @Override
26 public Project getProject(int id) {
27 return em.find(Project.class, id);
28 }
29
30 @Override
31 public List<Project> getProjects(int offset, int limit) {
32 TypedQuery<Project> query = em.createQuery("select t from Project t", Project.class);
33 if (offset>0) { query.setFirstResult(offset); }
34 if (limit>0) { query.setMaxResults(limit); }
35 return query.getResultList();
36 }
37
38 @Override
39 public void delete(Project task) {
40 em.remove(task);
41 }
42 }