From 249e6ad2c8f0a5af04f7e4aed42846dd8e0fc2de Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 28 Jan 2025 23:48:40 +0100 Subject: [PATCH] set JPAChecker agnostic of @ManyToOne primary key type --- src/org/kar/archidata/checker/CheckJPA.java | 90 ++++++--------------- 1 file changed, 23 insertions(+), 67 deletions(-) diff --git a/src/org/kar/archidata/checker/CheckJPA.java b/src/org/kar/archidata/checker/CheckJPA.java index ecb104b..ebf6609 100644 --- a/src/org/kar/archidata/checker/CheckJPA.java +++ b/src/org/kar/archidata/checker/CheckJPA.java @@ -12,7 +12,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.UUID; import java.util.regex.Pattern; import org.kar.archidata.annotation.AnnotationTools; @@ -223,31 +222,6 @@ public class CheckJPA implements CheckFunctionInterface { } }); } - final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field); - if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) { - add(fieldName, - ( - final DBAccess ioDb, - final String baseName, - final T data, - final List modifiedValue, - final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final List condCheckers = options.get(ConditionChecker.class); - final Condition conditionCheck = condCheckers.isEmpty() ? null - : condCheckers.get(0).toCondition(); - final long count = ioDb.count(annotationManyToOne.targetEntity(), elem, - conditionCheck); - if (count == 0) { - throw new InputException(baseName + fieldName, - "Foreign element does not exist in the DB:" + elem); - } - }); - } - } else if (type == Integer.class || type == int.class) { final DecimalMax maxValueDecimal = AnnotationTools.getConstraintsDecimalMax(field); if (maxValueDecimal != null) { @@ -346,47 +320,6 @@ public class CheckJPA implements CheckFunctionInterface { } }); } - final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field); - if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) { - add(fieldName, - ( - final DBAccess ioDb, - final String baseName, - final T data, - final List modifiedValue, - final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final long count = ioDb.count(annotationManyToOne.targetEntity(), elem); - if (count == 0) { - throw new InputException(baseName + fieldName, - "Foreign element does not exist in the DB:" + elem); - } - }); - } - } else if (type == UUID.class) { - final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field); - if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) { - add(fieldName, - ( - final DBAccess ioDb, - final String baseName, - final T data, - final List modifiedValue, - final QueryOptions options) -> { - final Object elem = field.get(data); - if (elem == null) { - return; - } - final long count = ioDb.count(annotationManyToOne.targetEntity(), elem); - if (count == 0) { - throw new InputException(baseName + fieldName, - "Foreign element does not exist in the DB:" + elem); - } - }); - } } else if (type == Boolean.class || type == boolean.class) { } else if (type == Float.class || type == float.class) { @@ -732,6 +665,29 @@ public class CheckJPA implements CheckFunctionInterface { } } } + final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field); + if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) { + add(fieldName, + ( + final DBAccess ioDb, + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + final Object elem = field.get(data); + if (elem == null) { + return; + } + final List condCheckers = options.get(ConditionChecker.class); + final Condition conditionCheck = condCheckers.isEmpty() ? null + : condCheckers.get(0).toCondition(); + final long count = ioDb.count(annotationManyToOne.targetEntity(), elem, conditionCheck); + if (count == 0) { + throw new InputException(baseName + fieldName, + "Foreign element does not exist in the DB:" + elem); + } + }); + } final CollectionItemUnique collectionUnique = AnnotationTools.getCollectionItemUnique(field); if (collectionUnique != null) { if (!Collection.class.isAssignableFrom(field.getType())) {