commit | author | age
|
aaf094
|
1 |
package com.redhat.training.services; |
RS |
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import javax.ejb.Stateless; |
|
6 |
import javax.persistence.EntityManager; |
|
7 |
import javax.persistence.PersistenceContext; |
|
8 |
import javax.persistence.TypedQuery; |
|
9 |
import javax.persistence.Query; |
|
10 |
|
|
11 |
import com.redhat.training.model.Person; |
|
12 |
|
|
13 |
@Stateless |
|
14 |
|
|
15 |
public class PersonService { |
|
16 |
|
|
17 |
@PersistenceContext(unitName="hello") |
|
18 |
private EntityManager entityManager; |
|
19 |
|
|
20 |
// Get all Person objects in the Database |
|
21 |
public List<Person> getAllPersons() { |
|
22 |
TypedQuery<Person> query = entityManager.createQuery("SELECT p FROM Person p", Person.class); |
|
23 |
return query.getResultList(); |
|
24 |
} |
|
25 |
/*//Get persons whose name matches the name given in the query -- Named parameters |
|
26 |
public List<Person> getPersonsWithName(String name) { |
|
27 |
TypedQuery<Person> query = entityManager.createQuery("SELECT p from Person p where p.name =:pname", Person.class); |
|
28 |
query.setParameter("pname", name); |
|
29 |
return = query.getResultList(); |
|
30 |
|
|
31 |
}*/ |
|
32 |
/* //Get persons whose name matches the name given in the query -- positional parameters |
|
33 |
public List<Person> getPersonsWithName(String name) { |
|
34 |
TypedQuery<Person> query = entityManager.createQuery("SELECT p from Person p where p.name =?1", Person.class); |
|
35 |
query.setParameter(1, name); |
|
36 |
return = query.getResultList(); |
|
37 |
}*/ |
|
38 |
//Get persons whose name matches the name given in the query |
|
39 |
public List<Person> getPersonsWithName(String name) { |
|
40 |
TypedQuery<Person> query = entityManager.createNamedQuery("getAllPersonsWithName", Person.class); |
|
41 |
query.setParameter("pname", name); |
|
42 |
return query.getResultList(); |
|
43 |
} |
|
44 |
} |