1 package ejava.examples.jndidemo.dao;
2
3 import java.util.List;
4
5
6 import javax.enterprise.inject.Typed;
7 import javax.inject.Inject;
8 import javax.persistence.EntityManager;
9
10
11 import ejava.examples.jndidemo.JndiDemo;
12 import ejava.examples.jndidemo.bo.Task;
13
14
15
16
17 @Typed({SchedulerDAO.class, JPASchedulerDAOImpl.class})
18 public class JPASchedulerDAOImpl extends JPADAOBase<Task> implements SchedulerDAO {
19
20 @Inject
21 public void setEntityManager(@JndiDemo EntityManager em) {
22 super.em = em;
23 }
24
25 public Task get(int id) {
26 return em.find(Task.class, id);
27 }
28
29 @Override
30 public Task findByName(String name) {
31 List<Task> tasks =
32 em.createQuery("select t from Task t where t.name=:name", Task.class)
33 .setParameter("name", name)
34 .getResultList();
35 return tasks.size()==0 ? null : tasks.get(0);
36 }
37
38 @Override
39 public List<Task> getTasks(int offset, int limit) {
40 return em.createQuery("select t from Task t", Task.class)
41 .setFirstResult(offset)
42 .setMaxResults(limit)
43 .getResultList();
44 }
45
46 @Override
47 public String toString() {
48 return new StringBuilder(super.toString())
49 .append(", em=" + em).toString();
50 }
51 }