HHH-16633 correct handling of null values in criteria-based @Find methods
This commit is contained in:
parent
78843fb2a9
commit
02e395c96d
|
@ -479,6 +479,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a finder method which returns multiple results.
|
||||
*/
|
||||
private void createCriteriaFinder(
|
||||
ExecutableElement method, TypeMirror returnType, @Nullable TypeElement containerType, TypeElement entity) {
|
||||
final String methodName = method.getSimpleName().toString();
|
||||
|
@ -496,6 +499,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
|||
containerType == null ? null : containerType.toString(),
|
||||
paramNames,
|
||||
paramTypes,
|
||||
false,
|
||||
dao,
|
||||
sessionType,
|
||||
enabledFetchProfiles( method )
|
||||
|
@ -550,6 +554,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
|||
null,
|
||||
paramNames,
|
||||
paramTypes,
|
||||
false,
|
||||
dao,
|
||||
sessionType,
|
||||
enabledFetchProfiles( method )
|
||||
|
@ -607,6 +612,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
|||
null,
|
||||
List.of( parameter.getSimpleName().toString() ),
|
||||
List.of( parameter.asType().toString() ),
|
||||
fieldType == FieldType.ID,
|
||||
dao,
|
||||
sessionType,
|
||||
enabledFetchProfiles( method )
|
||||
|
|
|
@ -11,6 +11,7 @@ import org.hibernate.jpamodelgen.model.Metamodel;
|
|||
import org.hibernate.jpamodelgen.util.Constants;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Gavin King
|
||||
|
@ -18,18 +19,21 @@ import java.util.List;
|
|||
public class CriteriaFinderMethod extends AbstractFinderMethod {
|
||||
|
||||
private final @Nullable String containerType;
|
||||
private final boolean isId;
|
||||
|
||||
public CriteriaFinderMethod(
|
||||
Metamodel annotationMetaEntity,
|
||||
String methodName, String entity,
|
||||
@Nullable String containerType,
|
||||
List<String> paramNames, List<String> paramTypes,
|
||||
boolean isId,
|
||||
boolean belongsToDao,
|
||||
String sessionType,
|
||||
List<String> fetchProfiles) {
|
||||
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, fetchProfiles,
|
||||
paramNames, paramTypes );
|
||||
this.containerType = containerType;
|
||||
this.isId = isId;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -43,7 +47,14 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
.append(methodName);
|
||||
parameters( declaration );
|
||||
declaration
|
||||
.append(" {")
|
||||
.append(" {");
|
||||
if ( isId ) {
|
||||
declaration
|
||||
.append("\n\tif (")
|
||||
.append(paramNames.get(0))
|
||||
.append(" == null) throw new IllegalArgumentException(\"Null identifier\");");
|
||||
}
|
||||
declaration
|
||||
.append("\n\tvar builder = entityManager")
|
||||
.append(usingEntityManager
|
||||
? ".getEntityManagerFactory()"
|
||||
|
@ -62,11 +73,24 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
.append(", ");
|
||||
}
|
||||
final String paramName = paramNames.get(i);
|
||||
final String paramType = paramTypes.get(i);
|
||||
declaration
|
||||
.append("\n\t\t\t");
|
||||
if ( !isId && !isPrimitive( paramType ) ) { //TODO: check the entity to see if it's @Basic(optional=false)
|
||||
declaration
|
||||
.append(paramName)
|
||||
.append("==null")
|
||||
.append("\n\t\t\t\t? ")
|
||||
.append("entity.get(");
|
||||
attributeRef( declaration, paramName );
|
||||
declaration
|
||||
.append(").isNull()")
|
||||
.append("\n\t\t\t\t: ");
|
||||
}
|
||||
declaration
|
||||
.append("builder.equal(entity.get(");
|
||||
attributeRef( declaration, paramName );
|
||||
declaration
|
||||
.append("\n\t\t\tbuilder.equal(entity.get(")
|
||||
.append(annotationMetaEntity.importType(entity + '_'))
|
||||
.append('.')
|
||||
.append(paramName)
|
||||
.append("), ")
|
||||
//TODO: only safe if we are binding literals as parameters!!!
|
||||
.append(paramName)
|
||||
|
@ -106,6 +130,20 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
return declaration.toString();
|
||||
}
|
||||
|
||||
private static boolean isPrimitive(String paramType) {
|
||||
return PRIMITIVE_TYPES.contains( paramType );
|
||||
}
|
||||
|
||||
private static final Set<String> PRIMITIVE_TYPES =
|
||||
Set.of("boolean", "char", "long", "int", "short", "byte", "double", "float");
|
||||
|
||||
private void attributeRef(StringBuilder declaration, String paramName) {
|
||||
declaration
|
||||
.append(annotationMetaEntity.importType(entity + '_'))
|
||||
.append('.')
|
||||
.append(paramName);
|
||||
}
|
||||
|
||||
private StringBuilder returnType() {
|
||||
StringBuilder type = new StringBuilder();
|
||||
if ( containerType != null ) {
|
||||
|
|
Loading…
Reference in New Issue