1
1
package com .bobocode .dao ;
2
2
3
- import com .bobocode .exception .AccountDaoException ;
4
3
import com .bobocode .model .Account ;
4
+ import com .bobocode .util .ExerciseNotCompletedException ;
5
5
6
- import javax .persistence .EntityManager ;
7
6
import javax .persistence .EntityManagerFactory ;
8
- import javax .persistence .TypedQuery ;
9
7
import java .util .List ;
10
- import java .util .function .Consumer ;
11
- import java .util .function .Function ;
12
8
13
9
public class AccountDaoImpl implements AccountDao {
14
10
private EntityManagerFactory emf ;
@@ -19,65 +15,32 @@ public AccountDaoImpl(EntityManagerFactory emf) {
19
15
20
16
@ Override
21
17
public void save (Account account ) {
22
- performWithinPersistenceContext ( em -> em . persist ( account ));
18
+ throw new ExerciseNotCompletedException (); // todo
23
19
}
24
20
25
21
@ Override
26
22
public Account findById (Long id ) {
27
- return performReturningWithinPersistenceContext ( entityManager -> entityManager . find ( Account . class , id ));
23
+ throw new ExerciseNotCompletedException (); // todo
28
24
}
29
25
30
26
@ Override
31
27
public Account findByEmail (String email ) {
32
- return performReturningWithinPersistenceContext (entityManager -> {
33
- TypedQuery <Account > findByEmailQuery
34
- = entityManager .createQuery ("select a from Account a where a.email = :email" , Account .class );
35
- findByEmailQuery .setParameter ("email" , email );
36
- return findByEmailQuery .getSingleResult ();
37
- });
28
+ throw new ExerciseNotCompletedException (); // todo
38
29
}
39
30
40
31
@ Override
41
32
public List <Account > findAll () {
42
- return performReturningWithinPersistenceContext (entityManager ->
43
- entityManager .createQuery ("select a from Account a" , Account .class ).getResultList ());
33
+ throw new ExerciseNotCompletedException (); // todo
44
34
}
45
35
46
36
@ Override
47
37
public void update (Account account ) {
48
- performWithinPersistenceContext ( em -> em . merge ( account ));
38
+ throw new ExerciseNotCompletedException (); // todo
49
39
}
50
40
51
41
@ Override
52
42
public void remove (Account account ) {
53
- performWithinPersistenceContext (entityManager -> {
54
- Account mergedAccount = entityManager .merge (account );
55
- entityManager .remove (mergedAccount );
56
- });
57
- }
58
-
59
- private void performWithinPersistenceContext (Consumer <EntityManager > entityManagerConsumer ) {
60
- performReturningWithinPersistenceContext (entityManager -> {
61
- entityManagerConsumer .accept (entityManager );
62
- return null ;
63
- });
64
- }
65
-
66
- private <T > T performReturningWithinPersistenceContext (Function <EntityManager , T > entityManagerFunction ) {
67
- EntityManager entityManager = emf .createEntityManager ();
68
- entityManager .getTransaction ().begin ();
69
- T result ;
70
- try {
71
- result = entityManagerFunction .apply (entityManager );
72
- entityManager .getTransaction ().commit ();
73
- } catch (Exception e ) {
74
- entityManager .getTransaction ().rollback ();
75
- throw new AccountDaoException ("Error performing dao operation. Transaction is rolled back!" , e );
76
- } finally {
77
- entityManager .close ();
78
- }
79
- return result ;
43
+ throw new ExerciseNotCompletedException (); // todo
80
44
}
81
45
}
82
46
83
-
0 commit comments