Skip to content

Commit

Permalink
Merge pull request #30213 from OpenLiberty/revert-30145-28595-empty-l…
Browse files Browse the repository at this point in the history
…ist-of-entities

Revert "empty list of entities"
  • Loading branch information
Zech-Hein authored Nov 18, 2024
2 parents 6215425 + 56e960f commit e74ea01
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ CWWKD1009.lifecycle.param.err.explanation=Lifecycle methods must have a single \
CWWKD1009.lifecycle.param.err.useraction=Update the repository method to have a \
single parameter that is an entity or an array of List of the entity.

CWWKD1010.unknown.entity.prop=CWWKD1010E: An entity property named {0} is \
not found on the {1} entity class for the {2} method of the {3} repository \
interface. Valid property names for the entity are: {4}.
CWWKD1010.unknown.entity.prop=CWWKD1010E: The {0} method of the {1} repository \
interface expects an entity property named {2} that is not found on the \
{3} entity class. Valid property names for the entity are: {4}.
CWWKD1010.unknown.entity.prop.explanation=The repository method is attempting \
to use an entity property that does not exist.
CWWKD1010.unknown.entity.prop.useraction=Update the repository method to match \
Expand Down Expand Up @@ -908,32 +908,3 @@ CWWKD1090.orderby.conflict.explanation=The OrderBy annotation is not compatible
with the OrderBy keyword.
CWWKD1090.orderby.conflict.useraction=Use only the OrderBy annotation or the \
OrderBy keyword.

CWWKD1091.method.name.parse.err=CWWKD1091E: An entity property named {0} is \
not found on the {1} entity class for the {2} method of the {3} repository \
interface. Confirm that the repository method either has a name that conforms \
to the Query by Method Name pattern or is annotated with one of: {4}. \
Valid property names for the entity are: {5}.
CWWKD1091.method.name.parse.err.explanation=The repository method name was parsed \
according to the Query by Method Name pattern because the repository method \
does not have a Jakarta Data annotation that indicates the type of operation.
CWWKD1091.method.name.parse.err.useraction=Correct the repository method name \
or annotate the repository method to indicate a Jakarta Data operation such as \
Query, Find, or Save.

CWWKD1092.lifecycle.arg.empty=CWWKD1092E: The {0} method of the {1} repository \
does not accept an empty {2} parameter. The parameter value must contain at \
least one entity.
CWWKD1092.lifecycle.arg.empty.explanation=Insert, Update, Save, and Delete methods
require at least one entity.
CWWKD1092.lifecycle.arg.empty.useraction=Ensure that the array, List, or Stream \
that is supplied to the method contains at least one entity.

CWWKD1093.fn.not.applicable=CWWKD1093E: The {0} function cannot be evaluated \
for the {1} entity that is used by the {2} method of the {3} repository \
interface. The entity does not have a property that is annotated with {4} \
or from which {0} can be inferred.
CWWKD1093.fn.not.applicable.explanation=The function cannot be used on the entity \
because the entity lacks an ID or version property.
CWWKD1093.fn.not.applicable.useraction=Do not use the function in query language \
or parameters for the repository method.
Original file line number Diff line number Diff line change
Expand Up @@ -1163,14 +1163,6 @@ Object findAndUpdate(Object arg, EntityManager em) throws Exception {
results.add(findAndUpdateOne(arg, em));
}
}

if (results.isEmpty())
throw exc(IllegalArgumentException.class,
"CWWKD1092.lifecycle.arg.empty",
method.getName(),
repositoryInterface.getName(),
method.getGenericParameterTypes()[0].getTypeName());

em.flush();

Class<?> returnType = method.getReturnType();
Expand Down Expand Up @@ -2464,23 +2456,15 @@ String getAttributeName(String name, boolean failIfNotFound) {
// id(this)
attributeName = entityInfo.attributeNames.get(By.ID);
if (attributeName == null && failIfNotFound)
throw exc(UnsupportedOperationException.class,
"CWWKD1093.fn.not.applicable",
name,
entityInfo.getType().getName(),
method.getName(),
repositoryInterface.getName(),
"@Id");
throw new MappingException("Entity class " + entityInfo.getType().getName() +
" does not have a property named " + name +
" or which is designated as the @Id."); // TODO NLS
} else if (len == 13 && name.regionMatches(true, 0, "version", 0, 7)) {
// version(this)
if (entityInfo.versionAttributeName == null && failIfNotFound)
throw exc(UnsupportedOperationException.class,
"CWWKD1093.fn.not.applicable",
name,
entityInfo.getType().getName(),
method.getName(),
repositoryInterface.getName(),
"@Version");
throw new MappingException("Entity class " + entityInfo.getType().getName() +
" does not have a property named " + name +
" or which is designated as the @Version."); // TODO NLS
else
attributeName = entityInfo.versionAttributeName;
} else {
Expand All @@ -2495,10 +2479,10 @@ String getAttributeName(String name, boolean failIfNotFound) {
else
throw exc(MappingException.class,
"CWWKD1010.unknown.entity.prop",
name,
entityInfo.getType().getName(),
method.getName(),
repositoryInterface.getName(),
name,
entityInfo.getType().getName(),
entityInfo.attributeTypes.keySet());
} else {
String lowerName = name.toLowerCase();
Expand All @@ -2520,23 +2504,15 @@ String getAttributeName(String name, boolean failIfNotFound) {
lowerName = lowerName.replace("_", "");
attributeName = entityInfo.attributeNames.get(lowerName);
if (attributeName == null && failIfNotFound) {
if (Util.hasOperationAnno(method))
throw exc(MappingException.class,
"CWWKD1010.unknown.entity.prop",
name,
entityInfo.getType().getName(),
method.getName(),
repositoryInterface.getName(),
entityInfo.attributeTypes.keySet());
else
throw exc(MappingException.class,
"CWWKD1091.method.name.parse.err",
name,
entityInfo.getType().getName(),
method.getName(),
repositoryInterface.getName(),
Util.OP_ANNOS,
entityInfo.attributeTypes.keySet());
// TODO If attempting to parse Query by Method Name without a By keyword, then the message
// should also include the possibility that repository method is missing an annotation.
throw exc(MappingException.class,
"CWWKD1010.unknown.entity.prop",
method.getName(),
repositoryInterface.getName(),
name,
entityInfo.getType().getName(),
entityInfo.attributeTypes.keySet());
}
}
}
Expand Down Expand Up @@ -3525,12 +3501,11 @@ Object insert(Object arg, EntityManager em) throws Exception {
ArrayList<Object> results;

boolean hasSingularEntityParam = false;
int entityCount = 0;
if (entityParamType.isArray()) {
int length = Array.getLength(arg);
results = resultVoid ? null : new ArrayList<>(length);
for (; entityCount < length; entityCount++) {
Object entity = toEntity(Array.get(arg, entityCount));
for (int i = 0; i < length; i++) {
Object entity = toEntity(Array.get(arg, i));
em.persist(entity);
if (results != null)
results.add(entity);
Expand All @@ -3544,15 +3519,13 @@ Object insert(Object arg, EntityManager em) throws Exception {
if (arg instanceof Iterable) {
results = resultVoid ? null : new ArrayList<>();
for (Object e : ((Iterable<?>) arg)) {
entityCount++;
Object entity = toEntity(e);
em.persist(entity);
if (results != null)
results.add(entity);
}
em.flush();
} else {
entityCount = 1;
hasSingularEntityParam = true;
results = resultVoid ? null : new ArrayList<>(1);
Object entity = toEntity(arg);
Expand All @@ -3563,13 +3536,6 @@ Object insert(Object arg, EntityManager em) throws Exception {
}
}

if (entityCount == 0)
throw exc(IllegalArgumentException.class,
"CWWKD1092.lifecycle.arg.empty",
method.getName(),
repositoryInterface.getName(),
method.getGenericParameterTypes()[0].getTypeName());

Class<?> returnType = method.getReturnType();
Object returnValue;
if (resultVoid) {
Expand Down Expand Up @@ -4032,12 +3998,11 @@ Object save(Object arg, EntityManager em) throws Exception {
List<Object> results;

boolean hasSingularEntityParam = false;
int entityCount = 0;
if (entityParamType.isArray()) {
results = new ArrayList<>();
int length = Array.getLength(arg);
for (; entityCount < length; entityCount++)
results.add(em.merge(toEntity(Array.get(arg, entityCount))));
for (int i = 0; i < length; i++)
results.add(em.merge(toEntity(Array.get(arg, i))));
em.flush();
} else {
arg = arg instanceof Stream //
Expand All @@ -4046,13 +4011,10 @@ Object save(Object arg, EntityManager em) throws Exception {

if (Iterable.class.isAssignableFrom(entityParamType)) {
results = new ArrayList<>();
for (Object e : ((Iterable<?>) arg)) {
entityCount++;
for (Object e : ((Iterable<?>) arg))
results.add(em.merge(toEntity(e)));
}
em.flush();
} else {
entityCount = 1;
hasSingularEntityParam = true;
results = resultVoid ? null : new ArrayList<>(1);
Object entity = em.merge(toEntity(arg));
Expand All @@ -4062,13 +4024,6 @@ Object save(Object arg, EntityManager em) throws Exception {
}
}

if (entityCount == 0)
throw exc(IllegalArgumentException.class,
"CWWKD1092.lifecycle.arg.empty",
method.getName(),
repositoryInterface.getName(),
method.getGenericParameterTypes()[0].getTypeName());

Class<?> returnType = method.getReturnType();
Object returnValue;
if (resultVoid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ else if (DoubleStream.class.equals(multiType))
queryInfo.returnArrayType.isInstance(firstNonNullResult)
|| queryInfo.returnArrayType.isPrimitive() &&
Util.isWrapperClassFor(queryInfo.returnArrayType,
firstNonNullResult.getClass())) {
firstNonNullResult.getClass())) {
returnValue = Array.newInstance(queryInfo.returnArrayType, size);
int i = 0;
for (Object result : results)
Expand Down Expand Up @@ -972,13 +972,6 @@ else if (DoubleStream.class.equals(multiType))
updateCount = queryInfo.remove(arg, em);
}

if (numExpected == 0)
throw exc(IllegalArgumentException.class,
"CWWKD1091.lifecycle.arg.empty",
method.getName(),
repositoryInterface.getName(),
method.getGenericParameterTypes()[0].getTypeName());

if (updateCount < numExpected)
if (numExpected == 1)
throw exc(OptimisticLockingFailureException.class,
Expand Down Expand Up @@ -1023,13 +1016,6 @@ else if (DoubleStream.class.equals(multiType))
updateCount = queryInfo.update(arg, em);
}

if (numExpected == 0)
throw exc(IllegalArgumentException.class,
"CWWKD1092.lifecycle.arg.empty",
method.getName(),
repositoryInterface.getName(),
method.getGenericParameterTypes()[0].getTypeName());

if (updateCount < numExpected)
if (numExpected == 1)
throw exc(OptimisticLockingFailureException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*******************************************************************************/
package io.openliberty.data.internal.persistence;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand All @@ -29,12 +27,6 @@
import jakarta.data.Order;
import jakarta.data.Sort;
import jakarta.data.page.PageRequest;
import jakarta.data.repository.Delete;
import jakarta.data.repository.Find;
import jakarta.data.repository.Insert;
import jakarta.data.repository.Query;
import jakarta.data.repository.Save;
import jakarta.data.repository.Update;

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

/**
* List of Jakarta Data operation annotations for use in NLS messages.
*/
static final String OP_ANNOS = "Delete, Find, Insert, Query, Save, Update";

/**
* Return types for deleteBy that distinguish delete-only from find-and-delete.
*/
Expand Down Expand Up @@ -133,31 +120,6 @@ public static boolean cannotBeEntity(Class<?> c) {
Modifier.isAbstract(modifiers);
}

/**
* Identifies whether a method is annotated with a Jakarta Data annotation
* that performs and operation, such as Query, Find, or Save. This method is
* for use by error reporting only, so it does not need to be very efficient.
*
* @param method repository method.
* @return if the repository method has an annotation indicating an operation.
*/
@Trivial
static final boolean hasOperationAnno(Method method) {
Set<Class<? extends Annotation>> operationAnnos = //
Set.of(Delete.class,
Insert.class,
Find.class,
Query.class,
Save.class,
Update.class);

for (Annotation anno : method.getAnnotations())
if (operationAnnos.contains(anno.annotationType()))
return true;

return false;
}

/**
* Indicates if the specified class is a wrapper for the primitive class.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5940,6 +5940,8 @@ public void testUpdateWithEntityParameter() {

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

assertEquals(0, persons.updateSome());

assertEquals(4, people.deleteBySSN_IdBetween(0L, 999999999L));
}

Expand Down
Loading

0 comments on commit e74ea01

Please sign in to comment.