@@ -28,13 +28,11 @@ public class BasicJpqlDao<P extends BasicJpa> implements BasicDao<P, EntityManag
2828 protected final EntityManagerFactory emf ;
2929 protected final Class <P > type ;
3030
31- @ Override
32- public P _getById (final Long id ) {
31+ P _getById (final Long id ) {
3332 return Transactions .withNewTransactionReturning (emf , em -> _getById (em , id ));
3433 }
3534
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 ,
3836 final String joinClause , final String whereClause , final ParamMap params , final String orderByClause ) {
3937 ListJson <P > r = new ListJson <>();
4038 r .setEntries (
@@ -47,33 +45,29 @@ public ListJson<P> getList(final EntityManager em, final long offset, final long
4745 return r ;
4846 }
4947
50- @ Override
51- public P create (final P entity ) {
48+ P create (final P entity ) {
5249 return Transactions .withNewTransactionReturning (emf , em -> create (em , entity ));
5350 }
5451
55- @ Override
56- public P create (final EntityManager em , final P entity ) {
52+ P create (final EntityManager em , final P entity ) {
5753 LocalDateTime time = DateUtils .nowUtc ();
5854 entity .setCreatedOn (time );
5955 entity .setEditedOn (time );
6056 em .persist (entity );
6157 return entity ;
6258 }
6359
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 ));
6762 }
6863
69- @ Override
70- public P update (final EntityManager em , final P entity ) {
64+ P _update (final EntityManager em , final P entity ) {
7165 LocalDateTime time = DateUtils .nowUtc ();
7266 entity .setEditedOn (time );
7367 return em .merge (entity );
7468 }
7569
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 ) {
7771 int s = Integer .MAX_VALUE ;
7872 if (size < s )
7973 s = (int ) size ;
@@ -85,24 +79,6 @@ public <T> List<T> getList(final EntityManager em, final TypedQuery<T> query, fi
8579 return query .getResultList ();
8680 }
8781
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-
10682 private <T > T getFirst (final EntityManager em , final TypedQuery <T > query ) {
10783 List <T > r = getList (em , query , 0 , 1 );
10884 if (r .size () == 1 ) {
@@ -112,24 +88,7 @@ private <T> T getFirst(final EntityManager em, final TypedQuery<T> query) {
11288 return null ;
11389 }
11490
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 ) {
13392 boolean wasInserted = false ;
13493 boolean wasUpdated = false ;
13594 P e = getFirst (em , query );
@@ -139,28 +98,25 @@ public UpsertResult<P> upsert(final EntityManager em, final TypedQuery<P> query,
13998 } else {
14099 entity .setId (e .getId ());
141100 entity .setCreatedOn (e .getCreatedOn ());
142- e = update (em , entity );
101+ e = _update (em , entity );
143102 wasUpdated = true ;
144103 }
145104 return UpsertResult .<P >builder ().wasInserted (wasInserted ).wasUpdated (wasUpdated ).jpa (e ).build ();
146105 }
147106
148- @ Override
149- public void _delete (final Long id ) {
107+ void _delete (final Long id ) {
150108 Transactions .withNewTransaction (emf , em -> {
151109 _delete (em , id );
152110 });
153111 }
154112
155- @ Override
156- public void _delete (final EntityManager em , final Long id ) {
113+ void _delete (final EntityManager em , final Long id ) {
157114 em .createQuery (String .format ("DELETE FROM %s AS o WHERE o.id = :id" , type .getSimpleName ()))
158115 .setParameter ("id" , id )
159116 .executeUpdate ();
160117 }
161118
162- @ Override
163- public P _getById (final EntityManager em , final Long id ) {
119+ P _getById (final EntityManager em , final Long id ) {
164120 try {
165121 return getQuery (em , "o" , null , "o.id = :id" , Map .of ("id" , id ), type , null , false , null ).getSingleResult ();
166122 } catch (NoResultException e ) {
@@ -278,6 +234,33 @@ else if (!orderBy.isBlank())
278234 return q ;
279235 }
280236
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+
281264 private boolean isAllowed (final info .unterrainer .commons .httpserver .daos .ListQueryBuilder query ,
282265 final EntityManager em ) {
283266 String tenantReferenceField = "testId" ;
0 commit comments