Skip to content

Commit f4d37d3

Browse files
Developer EnvironmentDeveloper Environment
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 f4d37d3

29 files changed

+1674
-736
lines changed

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

Lines changed: 179 additions & 69 deletions
Large diffs are not rendered by default.

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

Lines changed: 220 additions & 36 deletions
Large diffs are not rendered by default.

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

Lines changed: 74 additions & 13 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
/**
@@ -25,7 +26,7 @@
2526
* (read/write) within a transaction. Transactions are committed with the call to
2627
* {@link DatastoreTransaction#commit()} method. Transactions can be rolled back with a call to
2728
* {@link DatastoreTransaction#rollback()}.
28-
*
29+
*
2930
* @author Sai Pullabhotla
3031
*
3132
*/
@@ -34,7 +35,7 @@ public interface DatastoreTransaction extends DatastoreAccess {
3435
/**
3536
* Inserts the given entity. ID allocation is deferred to the submit time. Generated key, if any,
3637
* can be retrieved by calling {@link DatastoreTransaction.Response#getGeneratedKeys()}.
37-
*
38+
*
3839
* @param entity
3940
* the entity to insert.
4041
* @throws EntityManagerException
@@ -43,10 +44,24 @@ public interface DatastoreTransaction extends DatastoreAccess {
4344
*/
4445
<E> void insertWithDeferredIdAllocation(E entity);
4546

47+
/**
48+
* Inserts the given entity. ID allocation is deferred to the submit time. Generated key, if any,
49+
* can be retrieved by calling {@link DatastoreTransaction.Response#getGeneratedKeys()}.
50+
*
51+
* @param entity
52+
* the entity to insert.
53+
* @param entityType
54+
* the entity type
55+
* @throws EntityManagerException
56+
* if the entity has a String Identifier or any other error occurs while accessing the
57+
* underlying Datastore.
58+
*/
59+
<E> void insertWithDeferredIdAllocation(E entity, Type entityType);
60+
4661
/**
4762
* Inserts the given entities. ID allocation is deferred to the submit time. Generated keys, if
4863
* any, can be retrieved by calling {@link DatastoreTransaction.Response#getGeneratedKeys()}.
49-
*
64+
*
5065
* @param entities
5166
* the entities to insert
5267
* @throws EntityManagerException
@@ -55,12 +70,26 @@ public interface DatastoreTransaction extends DatastoreAccess {
5570
*/
5671
<E> void insertWithDeferredIdAllocation(List<E> entities);
5772

73+
/**
74+
* Inserts the given entities. ID allocation is deferred to the submit time. Generated keys, if
75+
* any, can be retrieved by calling {@link DatastoreTransaction.Response#getGeneratedKeys()}.
76+
*
77+
* @param entities
78+
* the entities to insert
79+
* @param entityType
80+
* the entity type
81+
* @throws EntityManagerException
82+
* if the entity has a String Identifier or any other error occurs while accessing the
83+
* underlying Datastore.
84+
*/
85+
<E> void insertWithDeferredIdAllocation(List<E> entities, Type entityType);
86+
5887
/**
5988
* Updates or inserts the given entity. ID allocation is deferred to the submit time. Generated
6089
* key, if any, can be retrieved by calling
6190
* {@link DatastoreTransaction.Response#getGeneratedKeys()}.
62-
*
63-
*
91+
*
92+
*
6493
* @param entity
6594
* the entity to update or insert.
6695
* @throws EntityManagerException
@@ -69,12 +98,28 @@ public interface DatastoreTransaction extends DatastoreAccess {
6998
*/
7099
<E> void upsertWithDeferredIdAllocation(E entity);
71100

101+
/**
102+
* Updates or inserts the given entity. ID allocation is deferred to the submit time. Generated
103+
* key, if any, can be retrieved by calling
104+
* {@link DatastoreTransaction.Response#getGeneratedKeys()}.
105+
*
106+
*
107+
* @param entity
108+
* the entity to update or insert.
109+
* @param entityType
110+
* the entity type
111+
* @throws EntityManagerException
112+
* if the entity has a String Identifier or any other error occurs while accessing the
113+
* underlying Datastore.
114+
*/
115+
<E> void upsertWithDeferredIdAllocation(E entity, Type entityType);
116+
72117
/**
73118
* Updates or Inserts the given entities. ID allocation is deferred to the submit time. Generated
74119
* keys, if any, can be retrieved by calling
75120
* {@link DatastoreTransaction.Response#getGeneratedKeys()}.
76-
*
77-
*
121+
*
122+
*
78123
* @param entities
79124
* the entities to update or insert
80125
* @throws EntityManagerException
@@ -83,27 +128,43 @@ public interface DatastoreTransaction extends DatastoreAccess {
83128
*/
84129
<E> void upsertWithDeferredIdAllocation(List<E> entities);
85130

131+
/**
132+
* Updates or Inserts the given entities. ID allocation is deferred to the submit time. Generated
133+
* keys, if any, can be retrieved by calling
134+
* {@link DatastoreTransaction.Response#getGeneratedKeys()}.
135+
*
136+
*
137+
* @param entities
138+
* the entities to update or insert
139+
* @param entityType
140+
* the entity type
141+
* @throws EntityManagerException
142+
* if the entity has a String Identifier or any other error occurs while accessing the
143+
* underlying Datastore.
144+
*/
145+
<E> void upsertWithDeferredIdAllocation(List<E> entities, Type entityType);
146+
86147
/**
87148
* Tells if this DatastoreTransaction is still active.
88-
*
149+
*
89150
* @return <code>true</code>, if this DatastoreTransaction is still active; <code>false</code>,
90151
* otherwise.
91152
*/
92153
boolean isActive();
93154

94155
/**
95156
* Commits changes made within this transaction.
96-
*
157+
*
97158
* @return Response. The response contains any generated.
98-
*
159+
*
99160
* @throws EntityManagerException
100161
* if the commit fails.
101162
*/
102163
Response commit();
103164

104165
/**
105166
* Rolls back the changes made in this transaction.
106-
*
167+
*
107168
* @throws EntityManagerException
108169
* if this transaction was already committed.
109170
*/
@@ -112,14 +173,14 @@ public interface DatastoreTransaction extends DatastoreAccess {
112173
/**
113174
* Transaction's commit Response. Used for returning generated keys for entities whose id
114175
* allocation was deferred until submit/commit time.
115-
*
176+
*
116177
* @author Sai Pullabhotla
117178
*
118179
*/
119180
interface Response {
120181
/**
121182
* Returns a list of generated keys.
122-
*
183+
*
123184
* @return a list of generated keys.
124185
*/
125186
List<DatastoreKey> getGeneratedKeys();

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

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,28 @@
1616

1717
package com.jmethods.catatumbo;
1818

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

2122
/**
2223
* Manages mapping and persistence of entities. EntityManager objects are created using the
2324
* {@link EntityManagerFactory}.
24-
*
25+
*
2526
* @author Sai Pullabhotla
2627
*
2728
*/
2829
public interface EntityManager extends DatastoreAccess {
2930

3031
/**
3132
* Deletes all entities of given Kind.
32-
*
33-
* @param entityClass
34-
* the entity class - The entity Kind will be determined from this class.
33+
*
34+
* @param entityType
35+
* the entity type - The entity Kind will be determined from this class.
3536
* @return the number of entities that were deleted
3637
* @throws EntityManagerException
3738
* if any error occurs while deleting.
3839
*/
39-
<E> long deleteAll(Class<E> entityClass);
40+
<E> long deleteAll(Type entityType);
4041

4142
/**
4243
* Deletes all entities of given Kind.
@@ -51,15 +52,15 @@ public interface EntityManager extends DatastoreAccess {
5152

5253
/**
5354
* Returns a new Transaction that can be used to perform a set of operations.
54-
*
55+
*
5556
* @return a new Transaction that can be used to perform a set of operations.
5657
*/
5758
DatastoreTransaction newTransaction();
5859

5960
/**
6061
* Creates and returns a new {@link DatastoreBatch} that can be used for processing multiple write
6162
* operations in one request.
62-
*
63+
*
6364
* @return a new <code>DatastoreBatch</code> for processing multiple write operations in one
6465
* request.
6566
*/
@@ -71,18 +72,18 @@ public interface EntityManager extends DatastoreAccess {
7172
* created {@link DatastoreTransaction} to perform reads/writes from/to the Cloud Datastore. When
7273
* the {@link TransactionalTask} finishes, the transaction is committed. If any error occurs
7374
* during the execution of the {@link TransactionalTask}, the transaction will be rolled back.
74-
*
75+
*
7576
* @param task
7677
* the task (or call back) to execute
7778
* @return the return value from the execution of
7879
* {@link TransactionalTask#execute(DatastoreTransaction)}.
79-
*
80+
*
8081
*/
8182
<T> T executeInTransaction(TransactionalTask<T> task);
8283

8384
/**
8485
* Registers the given entity lifecycle listeners with this entity manager.
85-
*
86+
*
8687
* @param classes
8788
* the classes that should receive entity lifecycle events. Lifecycle callbacks are
8889
* executed for all types of entities that are managed by this EntityManager.
@@ -91,15 +92,15 @@ public interface EntityManager extends DatastoreAccess {
9192

9293
/**
9394
* Returns the {@link DatastoreMetadata} object that can be used to retrieve metadata information.
94-
*
95+
*
9596
* @return the {@link DatastoreMetadata} object that can be used to retrieve metadata information.
9697
*/
9798
DatastoreMetadata getDatastoreMetadata();
9899

99100
/**
100101
* Returns the {@link DatastoreStats} object that can be used to retrieve various statistics on
101102
* the data stored in the Datastore.
102-
*
103+
*
103104
* @return the {@link DatastoreStats} object that can be used to retrieve various statistics on
104105
* the data stored in the Datastore.
105106
*/
@@ -108,7 +109,7 @@ public interface EntityManager extends DatastoreAccess {
108109
/**
109110
* Allocates IDs for the given entities and returns the allocated IDs. Each entity in the list
110111
* must have a its identifier of type numeric (long/Long).
111-
*
112+
*
112113
* @param entities
113114
* the entities
114115
* @return a list of {@link DatastoreKey}s.
@@ -120,10 +121,27 @@ public interface EntityManager extends DatastoreAccess {
120121
*/
121122
List<DatastoreKey> allocateId(List<Object> entities);
122123

124+
/**
125+
* Allocates IDs for the given entities and returns the allocated IDs. Each entity in the list
126+
* must have a its identifier of type numeric (long/Long).
127+
*
128+
* @param entities
129+
* the entities
130+
* @param entityType
131+
* the entity type
132+
* @return a list of {@link DatastoreKey}s.
133+
* @throws IllegalArgumentException
134+
* if any of the entities in the list do not have a numeric ID type or a valid ID is
135+
* already set.
136+
* @throws EntityManagerException
137+
* if any error occurs during key allocation
138+
*/
139+
List<DatastoreKey> allocateId(List<Object> entities, Type entityType);
140+
123141
/**
124142
* Allocates ID for the given entity and returns the allocated ID. The entity must have its
125143
* identifier of type numeric (long/Long).
126-
*
144+
*
127145
* @param entity
128146
* the the entity.
129147
* @return the allocated ID {@link DatastoreKey}.
@@ -135,4 +153,21 @@ public interface EntityManager extends DatastoreAccess {
135153
*/
136154
DatastoreKey allocateId(Object entity);
137155

156+
/**
157+
* Allocates ID for the given entity and returns the allocated ID. The entity must have its
158+
* identifier of type numeric (long/Long).
159+
*
160+
* @param entity
161+
* the the entity.
162+
* @param entityType
163+
* the entity type
164+
* @return the allocated ID {@link DatastoreKey}.
165+
* @throws IllegalArgumentException
166+
* if the ID type of the entity is not numeric, or if the entity has a valid ID
167+
* (non-null and non-zero).
168+
* @throws EntityManagerException
169+
* if any error occurs during ID allocation
170+
*/
171+
DatastoreKey allocateId(Object entity, Type entityType);
172+
138173
}

0 commit comments

Comments
 (0)