4
4
5
5
import javax .persistence .EntityManager ;
6
6
import javax .persistence .PersistenceContext ;
7
- import javax .persistence .Query ;
7
+ import javax .persistence .TypedQuery ;
8
8
9
9
import org .springframework .data .jpa .showcase .core .Customer ;
10
10
import org .springframework .stereotype .Repository ;
11
+ import org .springframework .transaction .annotation .Transactional ;
11
12
12
13
13
14
/**
16
17
* @author Oliver Gierke
17
18
*/
18
19
@ Repository
20
+ @ Transactional (readOnly = true )
19
21
public class CustomerServiceImpl implements CustomerService {
20
22
21
23
@ PersistenceContext
@@ -25,7 +27,9 @@ public class CustomerServiceImpl implements CustomerService {
25
27
/*
26
28
* (non-Javadoc)
27
29
*
28
- * @see org.geecon.hades.before.CustomerRepository#findById(java.lang.Long)
30
+ * @see
31
+ * org.springframework.data.jpa.showcase.before.CustomerService#findById
32
+ * (java.lang.Long)
29
33
*/
30
34
@ Override
31
35
public Customer findById (Long id ) {
@@ -37,26 +41,29 @@ public Customer findById(Long id) {
37
41
/*
38
42
* (non-Javadoc)
39
43
*
40
- * @see org.geecon.hades.before.CustomerRepository#findAll()
44
+ * @see
45
+ * org.springframework.data.jpa.showcase.before.CustomerService#findAll()
41
46
*/
42
47
@ Override
43
- @ SuppressWarnings ("unchecked" )
44
48
public List <Customer > findAll () {
45
49
46
- return em .createQuery ("from Customer c" ).getResultList ();
50
+ return em .createQuery ("select c from Customer c" , Customer .class )
51
+ .getResultList ();
47
52
}
48
53
49
54
50
55
/*
51
56
* (non-Javadoc)
52
57
*
53
- * @see org.geecon.hades.before.CustomerRepository#findAll(int, int)
58
+ * @see
59
+ * org.springframework.data.jpa.showcase.before.CustomerService#findAll(int,
60
+ * int)
54
61
*/
55
62
@ Override
56
- @ SuppressWarnings ("unchecked" )
57
63
public List <Customer > findAll (int page , int pageSize ) {
58
64
59
- Query query = em .createQuery ("from Customer c" );
65
+ TypedQuery <Customer > query =
66
+ em .createQuery ("select c from Customer c" , Customer .class );
60
67
61
68
query .setFirstResult (page * pageSize );
62
69
query .setMaxResults (pageSize );
@@ -69,10 +76,11 @@ public List<Customer> findAll(int page, int pageSize) {
69
76
* (non-Javadoc)
70
77
*
71
78
* @see
72
- * org.geecon.hades. before.CustomerRepository #save(org.geecon.hades.before
73
- * .Customer)
79
+ * org.springframework.data.jpa.showcase. before.CustomerService #save(org
80
+ * .springframework.data.jpa.showcase.core. Customer)
74
81
*/
75
82
@ Override
83
+ @ Transactional
76
84
public Customer save (Customer customer ) {
77
85
78
86
// Is new?
@@ -89,14 +97,16 @@ public Customer save(Customer customer) {
89
97
* (non-Javadoc)
90
98
*
91
99
* @see
92
- * org.geecon.hades. before.CustomerRepository #findByLastname(java.lang.String
93
- * , int, int)
100
+ * org.springframework.data.jpa.showcase. before.CustomerService #findByLastname
101
+ * (java.lang.String , int, int)
94
102
*/
95
103
@ Override
96
- @ SuppressWarnings ("unchecked" )
97
104
public List <Customer > findByLastname (String lastname , int page , int pageSize ) {
98
105
99
- Query query = em .createQuery ("from Customer c where c.lastname = ?" );
106
+ TypedQuery <Customer > query =
107
+ em .createQuery (
108
+ "select c from Customer c where c.lastname = ?1" ,
109
+ Customer .class );
100
110
101
111
query .setParameter (1 , lastname );
102
112
query .setFirstResult (page * pageSize );
0 commit comments