@@ -28,13 +28,11 @@ public class BasicJpqlDao<P extends BasicJpa> implements BasicDao<P, EntityManag
28
28
protected final EntityManagerFactory emf ;
29
29
protected final Class <P > type ;
30
30
31
- @ Override
32
- public P _getById (final Long id ) {
31
+ P _getById (final Long id ) {
33
32
return Transactions .withNewTransactionReturning (emf , em -> _getById (em , id ));
34
33
}
35
34
36
- @ Override
37
- public ListJson <P > getList (final EntityManager em , final long offset , final long size , final String selectClause ,
35
+ ListJson <P > getList (final EntityManager em , final long offset , final long size , final String selectClause ,
38
36
final String joinClause , final String whereClause , final ParamMap params , final String orderByClause ) {
39
37
ListJson <P > r = new ListJson <>();
40
38
r .setEntries (
@@ -47,33 +45,29 @@ public ListJson<P> getList(final EntityManager em, final long offset, final long
47
45
return r ;
48
46
}
49
47
50
- @ Override
51
- public P create (final P entity ) {
48
+ P create (final P entity ) {
52
49
return Transactions .withNewTransactionReturning (emf , em -> create (em , entity ));
53
50
}
54
51
55
- @ Override
56
- public P create (final EntityManager em , final P entity ) {
52
+ P create (final EntityManager em , final P entity ) {
57
53
LocalDateTime time = DateUtils .nowUtc ();
58
54
entity .setCreatedOn (time );
59
55
entity .setEditedOn (time );
60
56
em .persist (entity );
61
57
return entity ;
62
58
}
63
59
64
- @ Override
65
- public P update (final P entity ) {
66
- return Transactions .withNewTransactionReturning (emf , em -> update (em , entity ));
60
+ P _update (final P entity ) {
61
+ return Transactions .withNewTransactionReturning (emf , em -> _update (em , entity ));
67
62
}
68
63
69
- @ Override
70
- public P update (final EntityManager em , final P entity ) {
64
+ P _update (final EntityManager em , final P entity ) {
71
65
LocalDateTime time = DateUtils .nowUtc ();
72
66
entity .setEditedOn (time );
73
67
return em .merge (entity );
74
68
}
75
69
76
- public <T > List <T > getList (final EntityManager em , final TypedQuery <T > query , final long offset , final long size ) {
70
+ <T > List <T > getList (final EntityManager em , final TypedQuery <T > query , final long offset , final long size ) {
77
71
int s = Integer .MAX_VALUE ;
78
72
if (size < s )
79
73
s = (int ) size ;
@@ -85,24 +79,6 @@ public <T> List<T> getList(final EntityManager em, final TypedQuery<T> query, fi
85
79
return query .getResultList ();
86
80
}
87
81
88
- @ Override
89
- public UpsertResult <P > upsert (final String whereClause , final ParamMap params , final P entity ) {
90
- return Transactions .withNewTransactionReturning (emf , em -> upsert (em , getQuery (em , "o" , null , whereClause ,
91
- params == null ? null : params .getParameters (), type , null , false , null ), entity ));
92
- }
93
-
94
- @ Override
95
- public UpsertResult <P > upsert (final EntityManager em , final String whereClause , final ParamMap params ,
96
- final P entity ) {
97
- return upsert (em , getQuery (em , "o" , null , whereClause , params == null ? null : params .getParameters (), type ,
98
- null , false , null ), entity );
99
- }
100
-
101
- @ Override
102
- public UpsertResult <P > upsert (final TypedQuery <P > query , final P entity ) {
103
- return Transactions .withNewTransactionReturning (emf , em -> upsert (em , query , entity ));
104
- }
105
-
106
82
private <T > T getFirst (final EntityManager em , final TypedQuery <T > query ) {
107
83
List <T > r = getList (em , query , 0 , 1 );
108
84
if (r .size () == 1 ) {
@@ -112,24 +88,7 @@ private <T> T getFirst(final EntityManager em, final TypedQuery<T> query) {
112
88
return null ;
113
89
}
114
90
115
- public <T > UpsertResult <P > _upsert (final EntityManager em , final TypedQuery <T > query , final P entity ) {
116
- boolean wasInserted = false ;
117
- boolean wasUpdated = false ;
118
- T e = getFirst (em , query );
119
- if (e == null ) {
120
- e = create (em , entity );
121
- wasInserted = true ;
122
- } else {
123
- entity .setId (e .getId ());
124
- entity .setCreatedOn (e .getCreatedOn ());
125
- e = update (em , entity );
126
- wasUpdated = true ;
127
- }
128
- return UpsertResult .<P >builder ().wasInserted (wasInserted ).wasUpdated (wasUpdated ).jpa (e ).build ();
129
- }
130
-
131
- @ Override
132
- public UpsertResult <P > upsert (final EntityManager em , final TypedQuery <P > query , final P entity ) {
91
+ UpsertResult <P > _upsert (final EntityManager em , final TypedQuery <P > query , final P entity ) {
133
92
boolean wasInserted = false ;
134
93
boolean wasUpdated = false ;
135
94
P e = getFirst (em , query );
@@ -139,28 +98,25 @@ public UpsertResult<P> upsert(final EntityManager em, final TypedQuery<P> query,
139
98
} else {
140
99
entity .setId (e .getId ());
141
100
entity .setCreatedOn (e .getCreatedOn ());
142
- e = update (em , entity );
101
+ e = _update (em , entity );
143
102
wasUpdated = true ;
144
103
}
145
104
return UpsertResult .<P >builder ().wasInserted (wasInserted ).wasUpdated (wasUpdated ).jpa (e ).build ();
146
105
}
147
106
148
- @ Override
149
- public void _delete (final Long id ) {
107
+ void _delete (final Long id ) {
150
108
Transactions .withNewTransaction (emf , em -> {
151
109
_delete (em , id );
152
110
});
153
111
}
154
112
155
- @ Override
156
- public void _delete (final EntityManager em , final Long id ) {
113
+ void _delete (final EntityManager em , final Long id ) {
157
114
em .createQuery (String .format ("DELETE FROM %s AS o WHERE o.id = :id" , type .getSimpleName ()))
158
115
.setParameter ("id" , id )
159
116
.executeUpdate ();
160
117
}
161
118
162
- @ Override
163
- public P _getById (final EntityManager em , final Long id ) {
119
+ P _getById (final EntityManager em , final Long id ) {
164
120
try {
165
121
return getQuery (em , "o" , null , "o.id = :id" , Map .of ("id" , id ), type , null , false , null ).getSingleResult ();
166
122
} catch (NoResultException e ) {
@@ -278,6 +234,33 @@ else if (!orderBy.isBlank())
278
234
return q ;
279
235
}
280
236
237
+ <T > TypedQuery <T > getDeleteQuery (final EntityManager em , final String joinClause , final String whereClause ,
238
+ final Map <String , Object > params ) {
239
+ String query = "DELETE FROM %s AS o" ;
240
+
241
+ if (joinClause != null && !joinClause .isBlank ())
242
+ query += " " + joinClause ;
243
+
244
+ query += buildWhereClause (whereClause , null );
245
+
246
+ query = String .format (query , this .type .getSimpleName ());
247
+
248
+ String msg = query ;
249
+ if (params != null )
250
+ for (Entry <String , Object > p : params .entrySet ())
251
+ msg += "\\ n " + p .getKey () + ": " + p .getValue ();
252
+ log .debug (msg );
253
+
254
+ @ SuppressWarnings ("unchecked" )
255
+ Class <T > t = (Class <T >) this .type ;
256
+
257
+ TypedQuery <T > q = em .createQuery (query , t );
258
+ if (params != null )
259
+ for (Entry <String , Object > e : params .entrySet ())
260
+ q .setParameter (e .getKey (), e .getValue ());
261
+ return q ;
262
+ }
263
+
281
264
private boolean isAllowed (final info .unterrainer .commons .httpserver .daos .ListQueryBuilder query ,
282
265
final EntityManager em ) {
283
266
String tenantReferenceField = "testId" ;
0 commit comments