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