diff --git a/dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/ESContentletAPIImpl.java b/dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/ESContentletAPIImpl.java index 62a30fea992..14f321d47e3 100644 --- a/dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/ESContentletAPIImpl.java +++ b/dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/ESContentletAPIImpl.java @@ -8629,14 +8629,14 @@ private void validateRelationships(final Contentlet contentlet, if (!foundInRelationships && UtilMethods.isSet(contentlet.getIdentifier())) { // Check if there are existing related content records for this relationship try { - final List existingRelatedContent = getRelatedContent(contentlet, rel, - checkParent, APILocator.systemUser(), false); - hasExistingRelatedContent = existingRelatedContent != null && !existingRelatedContent.isEmpty(); + hasExistingRelatedContent = !FactoryLocator.getRelationshipFactory() + .dbRelatedContent(rel, contentlet, checkParent, false, null, 1, 0) + .isEmpty(); if (hasExistingRelatedContent) { Logger.debug(this, String.format("Required %s relationship [%s] not present in contentRelationships but found existing related content for contentlet [%s]", (checkParent ? "child" : "parent"), rel.getRelationTypeValue(), contentletId)); } - } catch (final DotDataException | DotSecurityException e) { + } catch (final DotDataException e) { Logger.error(this, String.format("Could not check existing related content for relationship [%s] and contentlet [%s]", rel.getRelationTypeValue(), contentletId), e); } @@ -8662,7 +8662,7 @@ private void validateRelationships(final Contentlet contentlet, } if (relationship.getCardinality() == RELATIONSHIP_CARDINALITY.ONE_TO_ONE - .ordinal() && contentsInRelationship.size() > 0) { + .ordinal() && !contentsInRelationship.isEmpty()) { hasError |= !isValidOneToOneRelationship(contentlet, builder, relationship, contentsInRelationship); @@ -8696,27 +8696,24 @@ private void validateRelationships(final Contentlet contentlet, } for (final Contentlet contentInRelationship : contentsInRelationship) { try { - // In order to get the related content we should use method getRelatedContent - // that has -boolean pullByParent- as parameter so we can pass -false- - // to get related content where we are parents. - final List relatedContents = getRelatedContent( - contentInRelationship, relationship, false, - APILocator.getUserAPI() - .getSystemUser(), true, 1, 0, null); - // If there's a 1-N relationship and the parent - // content is relating to a child that already has - // a parent... + // For ONE_TO_MANY, check if the child already has a different parent. + // Skip for other cardinalities — avoids N×M query explosion via the relationship cache. if (relationship.getCardinality() - == RELATIONSHIP_CARDINALITY.ONE_TO_MANY.ordinal() - && !relatedContents.isEmpty() - && !relatedContents.get(0).getIdentifier() - .equals(contentlet.getIdentifier())) { - final String errorMessage = String.format("ERROR! Parent content [%s] cannot be related to child content [%s] because it is already related to parent content [%s]", - contentletId, contentInRelationship.getIdentifier(), relatedContents.get(0).getIdentifier()); - Logger.error(this, errorMessage); - hasError = true; - builder.addBadCardinalityRelationship(relationship, - contentsInRelationship); + == RELATIONSHIP_CARDINALITY.ONE_TO_MANY.ordinal()) { + final List relatedContents = FactoryLocator + .getRelationshipFactory() + .dbRelatedContent(relationship, contentInRelationship, + false, false, null, 1, 0); + if (!relatedContents.isEmpty() + && !relatedContents.get(0).getIdentifier() + .equals(contentlet.getIdentifier())) { + final String errorMessage = String.format("ERROR! Parent content [%s] cannot be related to child content [%s] because it is already related to parent content [%s]", + contentletId, contentInRelationship.getIdentifier(), relatedContents.get(0).getIdentifier()); + Logger.error(this, errorMessage); + hasError = true; + builder.addBadCardinalityRelationship(relationship, + contentsInRelationship); + } } if (!contentInRelationship.getContentTypeId()