Skip to content

Commit d8c3646

Browse files
Developer EnvironmentAurelien Thieriot
authored andcommitted
Support for Generic entities
Update most of the entityManager methods to support receiving an extra ParameterizedType in case the user need to persist a Generic entity. The type lost by erasure will then be reified using the informations from the Parameterized type and the fields will be introspected as normal. Previous behaviour is not impacted and no breaking change was needed.
1 parent b0ab810 commit d8c3646

29 files changed

+1376
-438
lines changed

src/main/java/com/jmethods/catatumbo/DatastoreAccess.java

Lines changed: 150 additions & 40 deletions
Large diffs are not rendered by default.

src/main/java/com/jmethods/catatumbo/DatastoreBatch.java

Lines changed: 198 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.jmethods.catatumbo;
1818

19+
import java.lang.reflect.Type;
1920
import java.util.List;
2021

2122
/**
@@ -31,7 +32,7 @@ public interface DatastoreBatch {
3132
* Adds the given entity to this batch for insertion. If the entity does not have an identifier
3233
* set, ID may be automatically generated. If the same entity was added to this batch for
3334
* deletion, the insert request is changed into an upsert request.
34-
*
35+
*
3536
* @param entity
3637
* the entity to insert.
3738
* @return the inserted entity. The inserted entity may not be same as the passed in entity. For
@@ -41,6 +42,22 @@ public interface DatastoreBatch {
4142
*/
4243
<E> E insert(E entity);
4344

45+
/**
46+
* Adds the given entity to this batch for insertion. If the entity does not have an identifier
47+
* set, ID may be automatically generated. If the same entity was added to this batch for
48+
* deletion, the insert request is changed into an upsert request.
49+
*
50+
* @param entity
51+
* the entity to insert.
52+
* @param entityType
53+
* the entity type
54+
* @return the inserted entity. The inserted entity may not be same as the passed in entity. For
55+
* example, the inserted entity may contain any generated ID, key, parent key, etc.
56+
* @throws EntityManagerException
57+
* if any error occurs while inserting.
58+
*/
59+
<E> E insert(E entity, Type entityType);
60+
4461
/**
4562
* Adds the given entities to this batch for insertion. If any of the entities do not have an
4663
* identifier set, ID may be generated automatically. If any of entities were added to this batch
@@ -55,6 +72,22 @@ public interface DatastoreBatch {
5572
*/
5673
<E> List<E> insert(List<E> entities);
5774

75+
/**
76+
* Adds the given entities to this batch for insertion. If any of the entities do not have an
77+
* identifier set, ID may be generated automatically. If any of entities were added to this batch
78+
* for deletion, the insert request is changed to an upsert request.
79+
*
80+
* @param entities
81+
* the entities to insert.
82+
* @param entityType
83+
* the entity type
84+
* @return the inserted entities. The inserted entities may not be same as the passed in entities.
85+
* For example, the inserted entities may contain generated ID, key, parent key, etc.
86+
* @throws EntityManagerException
87+
* if any error occurs while inserting.
88+
*/
89+
<E> List<E> insert(List<E> entities, Type entityType);
90+
5891
/**
5992
* Adds the given entity to this batch for insertion. The ID allocation is deferred to the submit
6093
* time of this batch. Generated keys can be retrieved using
@@ -70,6 +103,23 @@ public interface DatastoreBatch {
70103
*/
71104
<E> void insertWithDeferredIdAllocation(E entity);
72105

106+
/**
107+
* Adds the given entity to this batch for insertion. The ID allocation is deferred to the submit
108+
* time of this batch. Generated keys can be retrieved using
109+
* {@link DatastoreBatch.Response#getGeneratedKeys()}.This method throws
110+
* {@link EntityManagerException} if the entity is using a String identifier. String identifiers
111+
* are allocated at add time, instead of submit time. For entities with String identifiers, use
112+
* the {@link DatastoreBatch#insert(Object)} method.
113+
*
114+
* @param entity
115+
* the entity to insert
116+
* @param entityType
117+
* the entity type
118+
* @throws EntityManagerException
119+
* if the entity has a String identifier or if any error occurs
120+
*/
121+
<E> void insertWithDeferredIdAllocation(E entity, Type entityType);
122+
73123
/**
74124
* Adds the given entities to this batch for insertion. The ID allocation is deferred to the
75125
* submit time of this batch. Generated keys can be retrieved using
@@ -85,6 +135,23 @@ public interface DatastoreBatch {
85135
*/
86136
<E> void insertWithDeferredIdAllocation(List<E> entities);
87137

138+
/**
139+
* Adds the given entities to this batch for insertion. The ID allocation is deferred to the
140+
* submit time of this batch. Generated keys can be retrieved using
141+
* {@link DatastoreBatch.Response#getGeneratedKeys()}.This method throws
142+
* {@link EntityManagerException} if the entity is using a String identifier. String identifiers
143+
* are allocated at add time, instead of submit time. For entities with String identifiers, use
144+
* the {@link DatastoreBatch#insert(List)} method.
145+
*
146+
* @param entities
147+
* the entities to insert
148+
* @param entityType
149+
* the entity type
150+
* @throws EntityManagerException
151+
* if the entity have a String identifier any error occurs
152+
*/
153+
<E> void insertWithDeferredIdAllocation(List<E> entities, Type entityType);
154+
88155
/**
89156
* Adds the given entity to this batch for update. The operation will fail if the entity with the
90157
* same key does not exist in the underlying Datastore. This method does not use optimistic
@@ -98,6 +165,21 @@ public interface DatastoreBatch {
98165
*/
99166
<E> E update(E entity);
100167

168+
/**
169+
* Adds the given entity to this batch for update. The operation will fail if the entity with the
170+
* same key does not exist in the underlying Datastore. This method does not use optimistic
171+
* locking even if the given entity has a field with {@link Version} annotation.
172+
*
173+
* @param entity
174+
* the entity to update
175+
* @param entityType
176+
* the entity type
177+
* @return the updated entity.
178+
* @throws EntityManagerException
179+
* if any error occurs while updating.
180+
*/
181+
<E> E update(E entity, Type entityType);
182+
101183
/**
102184
* Adds the given entities to this batch for update. The operation will fail if the entities with
103185
* the same key do not already exist in the underlying datastore. This method does not use
@@ -112,6 +194,22 @@ public interface DatastoreBatch {
112194
*/
113195
<E> List<E> update(List<E> entities);
114196

197+
/**
198+
* Adds the given entities to this batch for update. The operation will fail if the entities with
199+
* the same key do not already exist in the underlying datastore. This method does not use
200+
* optimistic locking even if the given entity has a field with {@link Version} annotation.
201+
*
202+
*
203+
* @param entities
204+
* the entities to update.
205+
* @param entityType
206+
* the entity type
207+
* @return the updated entities
208+
* @throws EntityManagerException
209+
* if any error occurs while inserting.
210+
*/
211+
<E> List<E> update(List<E> entities, Type entityType);
212+
115213
/**
116214
* Adds the given entity to this batch for update or insert. Any prior writes to this entity in
117215
* this batch will be removed from this batch. If the entity does not have an ID set, ID may be
@@ -125,6 +223,21 @@ public interface DatastoreBatch {
125223
*/
126224
<E> E upsert(E entity);
127225

226+
/**
227+
* Adds the given entity to this batch for update or insert. Any prior writes to this entity in
228+
* this batch will be removed from this batch. If the entity does not have an ID set, ID may be
229+
* generated automatically.
230+
*
231+
* @param entity
232+
* the entity to update or insert
233+
* @param entityType
234+
* the entity type
235+
* @return the updated/inserted entity.
236+
* @throws EntityManagerException
237+
* if any error occurs while saving.
238+
*/
239+
<E> E upsert(E entity, Type entityType);
240+
128241
/**
129242
* Adds the given entities to this batch for update or insert. Any prior writes of any of the
130243
* entities will be removed from this batch. If any of the entities do not have their IDs set, IDs
@@ -138,6 +251,21 @@ public interface DatastoreBatch {
138251
*/
139252
<E> List<E> upsert(List<E> entities);
140253

254+
/**
255+
* Adds the given entities to this batch for update or insert. Any prior writes of any of the
256+
* entities will be removed from this batch. If any of the entities do not have their IDs set, IDs
257+
* may be generated automatically.
258+
*
259+
* @param entities
260+
* the entities to update/or insert.
261+
* @param entityType
262+
* the entity type
263+
* @return the updated or inserted entities
264+
* @throws EntityManagerException
265+
* if any error occurs while saving.
266+
*/
267+
<E> List<E> upsert(List<E> entities, Type entityType);
268+
141269
/**
142270
* Adds the given entity to this batch for insert or update. The ID (for numeric ID) allocation
143271
* will be deferred to the submit time of this batch. This method throws an
@@ -152,6 +280,22 @@ public interface DatastoreBatch {
152280
*/
153281
<E> void upsertWithDeferredIdAllocation(E entity);
154282

283+
/**
284+
* Adds the given entity to this batch for insert or update. The ID (for numeric ID) allocation
285+
* will be deferred to the submit time of this batch. This method throws an
286+
* {@link EntityManagerException} if the entity is using a String ID. Entity with String ID should
287+
* use {@link DatastoreBatch#upsert(Object)} method.
288+
*
289+
* @param entity
290+
* the entity to update or insert
291+
* @param entityType
292+
* the entity type
293+
* @throws EntityManagerException
294+
* if the entity has a String ID or any other error occurs while accessing the
295+
* underlying datastore.
296+
*/
297+
<E> void upsertWithDeferredIdAllocation(E entity, Type entityType);
298+
155299
/**
156300
* Adds the given entities to this batch for update or insert. The ID (for numeric ID) allocation
157301
* will be deferred to the submit time of this batch. This method throws an
@@ -166,6 +310,22 @@ public interface DatastoreBatch {
166310
*/
167311
<E> void upsertWithDeferredIdAllocation(List<E> entities);
168312

313+
/**
314+
* Adds the given entities to this batch for update or insert. The ID (for numeric ID) allocation
315+
* will be deferred to the submit time of this batch. This method throws an
316+
* {@link EntityManagerException} if the entities have String identifiers. Entities with String
317+
* identifiers should use {@link DatastoreBatch#upsert(List)} method.
318+
*
319+
* @param entities
320+
* the entities to update or insert
321+
* @param entityType
322+
* the entity type
323+
* @throws EntityManagerException
324+
* if the entities have String identifiers or any other error occurs while accessing the
325+
* underlying datastore.
326+
*/
327+
<E> void upsertWithDeferredIdAllocation(List<E> entities, Type entityType);
328+
169329
/**
170330
* Adds the given entity to this batch for deletion.
171331
*
@@ -176,6 +336,18 @@ public interface DatastoreBatch {
176336
*/
177337
void delete(Object entity);
178338

339+
/**
340+
* Adds the given entity to this batch for deletion.
341+
*
342+
* @param entity
343+
* the entity to delete.
344+
* @param entityType
345+
* the entity type
346+
* @throws EntityManagerException
347+
* if any error occurs while deleting.
348+
*/
349+
void delete(Object entity, Type entityType);
350+
179351
/**
180352
* Adds the given entities to this batch for deletion.
181353
*
@@ -186,57 +358,69 @@ public interface DatastoreBatch {
186358
*/
187359
void delete(List<?> entities);
188360

361+
/**
362+
* Adds the given entities to this batch for deletion.
363+
*
364+
* @param entities
365+
* the entities to delete.
366+
* @param entityType
367+
* the entity type
368+
* @throws EntityManagerException
369+
* if any error occurs while deleting.
370+
*/
371+
void delete(List<?> entities, Type entityType);
372+
189373
/**
190374
* Adds the given ID and entity type to this batch for deletion.
191375
*
192-
* @param entityClass
193-
* the entity class.
376+
* @param entityType
377+
* the entity type.
194378
* @param id
195379
* the ID of the entity.
196380
* @throws EntityManagerException
197381
* if any error occurs while inserting.
198382
*/
199-
<E> void delete(Class<E> entityClass, long id);
383+
<E> void delete(Type entityType, long id);
200384

201385
/**
202386
* Adds the given ID and entity type to this batch for deletion.
203387
*
204-
* @param entityClass
205-
* the entity class.
388+
* @param entityType
389+
* the entity type.
206390
* @param id
207391
* the ID of the entity.
208392
* @throws EntityManagerException
209393
* if any error occurs while inserting.
210394
*/
211-
<E> void delete(Class<E> entityClass, String id);
395+
<E> void delete(Type entityType, String id);
212396

213397
/**
214398
* Adds the given entity type, parent key and ID to this batch for deletion.
215399
*
216-
* @param entityClass
217-
* the entity class.
400+
* @param entityType
401+
* the entity type.
218402
* @param parentKey
219403
* the parent key
220404
* @param id
221405
* the ID of the entity.
222406
* @throws EntityManagerException
223407
* if any error occurs while inserting.
224408
*/
225-
<E> void delete(Class<E> entityClass, DatastoreKey parentKey, long id);
409+
<E> void delete(Type entityType, DatastoreKey parentKey, long id);
226410

227411
/**
228412
* Adds the given entity type, parent key and ID to this batch for deletion.
229413
*
230-
* @param entityClass
231-
* the entity class.
414+
* @param entityType
415+
* the entity type.
232416
* @param parentKey
233417
* the parent key
234418
* @param id
235419
* the ID of the entity.
236420
* @throws EntityManagerException
237421
* if any error occurs while inserting.
238422
*/
239-
<E> void delete(Class<E> entityClass, DatastoreKey parentKey, String id);
423+
<E> void delete(Type entityType, DatastoreKey parentKey, String id);
240424

241425
/**
242426
* Adds the given key to this batch for deletion.
@@ -250,7 +434,7 @@ public interface DatastoreBatch {
250434

251435
/**
252436
* Adds the given keys to this batch for deletion.
253-
*
437+
*
254438
* @param keys
255439
* the keys to delete
256440
* @throws EntityManagerException

0 commit comments

Comments
 (0)