Skip to content

Commit e74ea01

Browse files
authored
Merge pull request #30213 from OpenLiberty/revert-30145-28595-empty-list-of-entities
Revert "empty list of entities"
2 parents 6215425 + 56e960f commit e74ea01

File tree

8 files changed

+30
-228
lines changed

8 files changed

+30
-228
lines changed

dev/io.openliberty.data.internal.persistence/resources/io/openliberty/data/internal/persistence/resources/CWWKDMessages.nlsprops

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ CWWKD1009.lifecycle.param.err.explanation=Lifecycle methods must have a single \
125125
CWWKD1009.lifecycle.param.err.useraction=Update the repository method to have a \
126126
single parameter that is an entity or an array of List of the entity.
127127

128-
CWWKD1010.unknown.entity.prop=CWWKD1010E: An entity property named {0} is \
129-
not found on the {1} entity class for the {2} method of the {3} repository \
130-
interface. Valid property names for the entity are: {4}.
128+
CWWKD1010.unknown.entity.prop=CWWKD1010E: The {0} method of the {1} repository \
129+
interface expects an entity property named {2} that is not found on the \
130+
{3} entity class. Valid property names for the entity are: {4}.
131131
CWWKD1010.unknown.entity.prop.explanation=The repository method is attempting \
132132
to use an entity property that does not exist.
133133
CWWKD1010.unknown.entity.prop.useraction=Update the repository method to match \
@@ -908,32 +908,3 @@ CWWKD1090.orderby.conflict.explanation=The OrderBy annotation is not compatible
908908
with the OrderBy keyword.
909909
CWWKD1090.orderby.conflict.useraction=Use only the OrderBy annotation or the \
910910
OrderBy keyword.
911-
912-
CWWKD1091.method.name.parse.err=CWWKD1091E: An entity property named {0} is \
913-
not found on the {1} entity class for the {2} method of the {3} repository \
914-
interface. Confirm that the repository method either has a name that conforms \
915-
to the Query by Method Name pattern or is annotated with one of: {4}. \
916-
Valid property names for the entity are: {5}.
917-
CWWKD1091.method.name.parse.err.explanation=The repository method name was parsed \
918-
according to the Query by Method Name pattern because the repository method \
919-
does not have a Jakarta Data annotation that indicates the type of operation.
920-
CWWKD1091.method.name.parse.err.useraction=Correct the repository method name \
921-
or annotate the repository method to indicate a Jakarta Data operation such as \
922-
Query, Find, or Save.
923-
924-
CWWKD1092.lifecycle.arg.empty=CWWKD1092E: The {0} method of the {1} repository \
925-
does not accept an empty {2} parameter. The parameter value must contain at \
926-
least one entity.
927-
CWWKD1092.lifecycle.arg.empty.explanation=Insert, Update, Save, and Delete methods
928-
require at least one entity.
929-
CWWKD1092.lifecycle.arg.empty.useraction=Ensure that the array, List, or Stream \
930-
that is supplied to the method contains at least one entity.
931-
932-
CWWKD1093.fn.not.applicable=CWWKD1093E: The {0} function cannot be evaluated \
933-
for the {1} entity that is used by the {2} method of the {3} repository \
934-
interface. The entity does not have a property that is annotated with {4} \
935-
or from which {0} can be inferred.
936-
CWWKD1093.fn.not.applicable.explanation=The function cannot be used on the entity \
937-
because the entity lacks an ID or version property.
938-
CWWKD1093.fn.not.applicable.useraction=Do not use the function in query language \
939-
or parameters for the repository method.

dev/io.openliberty.data.internal.persistence/src/io/openliberty/data/internal/persistence/QueryInfo.java

Lines changed: 22 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,14 +1163,6 @@ Object findAndUpdate(Object arg, EntityManager em) throws Exception {
11631163
results.add(findAndUpdateOne(arg, em));
11641164
}
11651165
}
1166-
1167-
if (results.isEmpty())
1168-
throw exc(IllegalArgumentException.class,
1169-
"CWWKD1092.lifecycle.arg.empty",
1170-
method.getName(),
1171-
repositoryInterface.getName(),
1172-
method.getGenericParameterTypes()[0].getTypeName());
1173-
11741166
em.flush();
11751167

11761168
Class<?> returnType = method.getReturnType();
@@ -2464,23 +2456,15 @@ String getAttributeName(String name, boolean failIfNotFound) {
24642456
// id(this)
24652457
attributeName = entityInfo.attributeNames.get(By.ID);
24662458
if (attributeName == null && failIfNotFound)
2467-
throw exc(UnsupportedOperationException.class,
2468-
"CWWKD1093.fn.not.applicable",
2469-
name,
2470-
entityInfo.getType().getName(),
2471-
method.getName(),
2472-
repositoryInterface.getName(),
2473-
"@Id");
2459+
throw new MappingException("Entity class " + entityInfo.getType().getName() +
2460+
" does not have a property named " + name +
2461+
" or which is designated as the @Id."); // TODO NLS
24742462
} else if (len == 13 && name.regionMatches(true, 0, "version", 0, 7)) {
24752463
// version(this)
24762464
if (entityInfo.versionAttributeName == null && failIfNotFound)
2477-
throw exc(UnsupportedOperationException.class,
2478-
"CWWKD1093.fn.not.applicable",
2479-
name,
2480-
entityInfo.getType().getName(),
2481-
method.getName(),
2482-
repositoryInterface.getName(),
2483-
"@Version");
2465+
throw new MappingException("Entity class " + entityInfo.getType().getName() +
2466+
" does not have a property named " + name +
2467+
" or which is designated as the @Version."); // TODO NLS
24842468
else
24852469
attributeName = entityInfo.versionAttributeName;
24862470
} else {
@@ -2495,10 +2479,10 @@ String getAttributeName(String name, boolean failIfNotFound) {
24952479
else
24962480
throw exc(MappingException.class,
24972481
"CWWKD1010.unknown.entity.prop",
2498-
name,
2499-
entityInfo.getType().getName(),
25002482
method.getName(),
25012483
repositoryInterface.getName(),
2484+
name,
2485+
entityInfo.getType().getName(),
25022486
entityInfo.attributeTypes.keySet());
25032487
} else {
25042488
String lowerName = name.toLowerCase();
@@ -2520,23 +2504,15 @@ String getAttributeName(String name, boolean failIfNotFound) {
25202504
lowerName = lowerName.replace("_", "");
25212505
attributeName = entityInfo.attributeNames.get(lowerName);
25222506
if (attributeName == null && failIfNotFound) {
2523-
if (Util.hasOperationAnno(method))
2524-
throw exc(MappingException.class,
2525-
"CWWKD1010.unknown.entity.prop",
2526-
name,
2527-
entityInfo.getType().getName(),
2528-
method.getName(),
2529-
repositoryInterface.getName(),
2530-
entityInfo.attributeTypes.keySet());
2531-
else
2532-
throw exc(MappingException.class,
2533-
"CWWKD1091.method.name.parse.err",
2534-
name,
2535-
entityInfo.getType().getName(),
2536-
method.getName(),
2537-
repositoryInterface.getName(),
2538-
Util.OP_ANNOS,
2539-
entityInfo.attributeTypes.keySet());
2507+
// TODO If attempting to parse Query by Method Name without a By keyword, then the message
2508+
// should also include the possibility that repository method is missing an annotation.
2509+
throw exc(MappingException.class,
2510+
"CWWKD1010.unknown.entity.prop",
2511+
method.getName(),
2512+
repositoryInterface.getName(),
2513+
name,
2514+
entityInfo.getType().getName(),
2515+
entityInfo.attributeTypes.keySet());
25402516
}
25412517
}
25422518
}
@@ -3525,12 +3501,11 @@ Object insert(Object arg, EntityManager em) throws Exception {
35253501
ArrayList<Object> results;
35263502

35273503
boolean hasSingularEntityParam = false;
3528-
int entityCount = 0;
35293504
if (entityParamType.isArray()) {
35303505
int length = Array.getLength(arg);
35313506
results = resultVoid ? null : new ArrayList<>(length);
3532-
for (; entityCount < length; entityCount++) {
3533-
Object entity = toEntity(Array.get(arg, entityCount));
3507+
for (int i = 0; i < length; i++) {
3508+
Object entity = toEntity(Array.get(arg, i));
35343509
em.persist(entity);
35353510
if (results != null)
35363511
results.add(entity);
@@ -3544,15 +3519,13 @@ Object insert(Object arg, EntityManager em) throws Exception {
35443519
if (arg instanceof Iterable) {
35453520
results = resultVoid ? null : new ArrayList<>();
35463521
for (Object e : ((Iterable<?>) arg)) {
3547-
entityCount++;
35483522
Object entity = toEntity(e);
35493523
em.persist(entity);
35503524
if (results != null)
35513525
results.add(entity);
35523526
}
35533527
em.flush();
35543528
} else {
3555-
entityCount = 1;
35563529
hasSingularEntityParam = true;
35573530
results = resultVoid ? null : new ArrayList<>(1);
35583531
Object entity = toEntity(arg);
@@ -3563,13 +3536,6 @@ Object insert(Object arg, EntityManager em) throws Exception {
35633536
}
35643537
}
35653538

3566-
if (entityCount == 0)
3567-
throw exc(IllegalArgumentException.class,
3568-
"CWWKD1092.lifecycle.arg.empty",
3569-
method.getName(),
3570-
repositoryInterface.getName(),
3571-
method.getGenericParameterTypes()[0].getTypeName());
3572-
35733539
Class<?> returnType = method.getReturnType();
35743540
Object returnValue;
35753541
if (resultVoid) {
@@ -4032,12 +3998,11 @@ Object save(Object arg, EntityManager em) throws Exception {
40323998
List<Object> results;
40333999

40344000
boolean hasSingularEntityParam = false;
4035-
int entityCount = 0;
40364001
if (entityParamType.isArray()) {
40374002
results = new ArrayList<>();
40384003
int length = Array.getLength(arg);
4039-
for (; entityCount < length; entityCount++)
4040-
results.add(em.merge(toEntity(Array.get(arg, entityCount))));
4004+
for (int i = 0; i < length; i++)
4005+
results.add(em.merge(toEntity(Array.get(arg, i))));
40414006
em.flush();
40424007
} else {
40434008
arg = arg instanceof Stream //
@@ -4046,13 +4011,10 @@ Object save(Object arg, EntityManager em) throws Exception {
40464011

40474012
if (Iterable.class.isAssignableFrom(entityParamType)) {
40484013
results = new ArrayList<>();
4049-
for (Object e : ((Iterable<?>) arg)) {
4050-
entityCount++;
4014+
for (Object e : ((Iterable<?>) arg))
40514015
results.add(em.merge(toEntity(e)));
4052-
}
40534016
em.flush();
40544017
} else {
4055-
entityCount = 1;
40564018
hasSingularEntityParam = true;
40574019
results = resultVoid ? null : new ArrayList<>(1);
40584020
Object entity = em.merge(toEntity(arg));
@@ -4062,13 +4024,6 @@ Object save(Object arg, EntityManager em) throws Exception {
40624024
}
40634025
}
40644026

4065-
if (entityCount == 0)
4066-
throw exc(IllegalArgumentException.class,
4067-
"CWWKD1092.lifecycle.arg.empty",
4068-
method.getName(),
4069-
repositoryInterface.getName(),
4070-
method.getGenericParameterTypes()[0].getTypeName());
4071-
40724027
Class<?> returnType = method.getReturnType();
40734028
Object returnValue;
40744029
if (resultVoid) {

dev/io.openliberty.data.internal.persistence/src/io/openliberty/data/internal/persistence/RepositoryImpl.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ else if (DoubleStream.class.equals(multiType))
847847
queryInfo.returnArrayType.isInstance(firstNonNullResult)
848848
|| queryInfo.returnArrayType.isPrimitive() &&
849849
Util.isWrapperClassFor(queryInfo.returnArrayType,
850-
firstNonNullResult.getClass())) {
850+
firstNonNullResult.getClass())) {
851851
returnValue = Array.newInstance(queryInfo.returnArrayType, size);
852852
int i = 0;
853853
for (Object result : results)
@@ -972,13 +972,6 @@ else if (DoubleStream.class.equals(multiType))
972972
updateCount = queryInfo.remove(arg, em);
973973
}
974974

975-
if (numExpected == 0)
976-
throw exc(IllegalArgumentException.class,
977-
"CWWKD1091.lifecycle.arg.empty",
978-
method.getName(),
979-
repositoryInterface.getName(),
980-
method.getGenericParameterTypes()[0].getTypeName());
981-
982975
if (updateCount < numExpected)
983976
if (numExpected == 1)
984977
throw exc(OptimisticLockingFailureException.class,
@@ -1023,13 +1016,6 @@ else if (DoubleStream.class.equals(multiType))
10231016
updateCount = queryInfo.update(arg, em);
10241017
}
10251018

1026-
if (numExpected == 0)
1027-
throw exc(IllegalArgumentException.class,
1028-
"CWWKD1092.lifecycle.arg.empty",
1029-
method.getName(),
1030-
repositoryInterface.getName(),
1031-
method.getGenericParameterTypes()[0].getTypeName());
1032-
10331019
if (updateCount < numExpected)
10341020
if (numExpected == 1)
10351021
throw exc(OptimisticLockingFailureException.class,

dev/io.openliberty.data.internal.persistence/src/io/openliberty/data/internal/persistence/Util.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
*******************************************************************************/
1010
package io.openliberty.data.internal.persistence;
1111

12-
import java.lang.annotation.Annotation;
13-
import java.lang.reflect.Method;
1412
import java.lang.reflect.Modifier;
1513
import java.math.BigDecimal;
1614
import java.math.BigInteger;
@@ -29,12 +27,6 @@
2927
import jakarta.data.Order;
3028
import jakarta.data.Sort;
3129
import jakarta.data.page.PageRequest;
32-
import jakarta.data.repository.Delete;
33-
import jakarta.data.repository.Find;
34-
import jakarta.data.repository.Insert;
35-
import jakarta.data.repository.Query;
36-
import jakarta.data.repository.Save;
37-
import jakarta.data.repository.Update;
3830

3931
/**
4032
* A location for helper methods that do not require any state.
@@ -52,11 +44,6 @@ public class Util {
5244
Set.of(long.class, int.class, short.class, byte.class,
5345
double.class, float.class);
5446

55-
/**
56-
* List of Jakarta Data operation annotations for use in NLS messages.
57-
*/
58-
static final String OP_ANNOS = "Delete, Find, Insert, Query, Save, Update";
59-
6047
/**
6148
* Return types for deleteBy that distinguish delete-only from find-and-delete.
6249
*/
@@ -133,31 +120,6 @@ public static boolean cannotBeEntity(Class<?> c) {
133120
Modifier.isAbstract(modifiers);
134121
}
135122

136-
/**
137-
* Identifies whether a method is annotated with a Jakarta Data annotation
138-
* that performs and operation, such as Query, Find, or Save. This method is
139-
* for use by error reporting only, so it does not need to be very efficient.
140-
*
141-
* @param method repository method.
142-
* @return if the repository method has an annotation indicating an operation.
143-
*/
144-
@Trivial
145-
static final boolean hasOperationAnno(Method method) {
146-
Set<Class<? extends Annotation>> operationAnnos = //
147-
Set.of(Delete.class,
148-
Insert.class,
149-
Find.class,
150-
Query.class,
151-
Save.class,
152-
Update.class);
153-
154-
for (Annotation anno : method.getAnnotations())
155-
if (operationAnnos.contains(anno.annotationType()))
156-
return true;
157-
158-
return false;
159-
}
160-
161123
/**
162124
* Indicates if the specified class is a wrapper for the primitive class.
163125
*

dev/io.openliberty.data.internal_fat/test-applications/DataTestApp/src/test/jakarta/data/web/DataTestServlet.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5940,6 +5940,8 @@ public void testUpdateWithEntityParameter() {
59405940

59415941
assertEquals(3, persons.updateSome(ulysses, ursula, uriah));
59425942

5943+
assertEquals(0, persons.updateSome());
5944+
59435945
assertEquals(4, people.deleteBySSN_IdBetween(0L, 999999999L));
59445946
}
59455947

0 commit comments

Comments
 (0)