From ceadea90cc5741e1aeb4705b7e78d7502274c798 Mon Sep 17 00:00:00 2001 From: Marcelo Boveto Shima Date: Mon, 19 Feb 2024 22:08:47 -0300 Subject: [PATCH] reorganize relationship preparation --- .../support/prepare-relationship.js | 152 +++++++++--------- generators/liquibase/generator.ts | 8 + .../web/rest/_entityClass_ResourceIT.java.ejs | 2 +- ...istClass_.java.jhi.jakarta_persistence.ejs | 2 +- 4 files changed, 86 insertions(+), 78 deletions(-) diff --git a/generators/base-application/support/prepare-relationship.js b/generators/base-application/support/prepare-relationship.js index e0d9e0a9d6fa..d72a6c26d726 100644 --- a/generators/base-application/support/prepare-relationship.js +++ b/generators/base-application/support/prepare-relationship.js @@ -40,7 +40,7 @@ function _defineOnUpdateAndOnDelete(relationship, generator) { export default function prepareRelationship(entityWithConfig, relationship, generator, ignoreMissingRequiredRelationship) { const entityName = entityWithConfig.name; - const otherEntityName = relationship.otherEntityName; + const { otherEntityName, relationshipSide, relationshipType, relationshipName } = relationship; if (!relationship.otherEntity) { throw new Error( @@ -52,25 +52,61 @@ export default function prepareRelationship(entityWithConfig, relationship, gene relationship.otherEntityField = otherEntityData.primaryKey.name; } + // Prepare basic relationship data Object.assign(relationship, { - relationshipLeftSide: relationship.relationshipSide === 'left', - relationshipRightSide: relationship.relationshipSide === 'right', - collection: relationship.relationshipType === 'one-to-many' || relationship.relationshipType === 'many-to-many', - relationshipOneToOne: relationship.relationshipType === 'one-to-one', - relationshipOneToMany: relationship.relationshipType === 'one-to-many', - relationshipManyToOne: relationship.relationshipType === 'many-to-one', - relationshipManyToMany: relationship.relationshipType === 'many-to-many', - otherEntityUser: relationship.otherEntityName === 'user', + relationshipLeftSide: relationshipSide === 'left', + relationshipRightSide: relationshipSide === 'right', + collection: relationshipType === 'one-to-many' || relationshipType === 'many-to-many', + relationshipOneToOne: relationshipType === 'one-to-one', + relationshipOneToMany: relationshipType === 'one-to-many', + relationshipManyToOne: relationshipType === 'many-to-one', + relationshipManyToMany: relationshipType === 'many-to-many', + otherEntityUser: otherEntityName === 'user', }); + const { collection, relationshipLeftSide, relationshipManyToOne, relationshipOneToMany, relationshipOneToOne, relationshipManyToMany } = + relationship; + + // Prepare property name + mutateData(relationship, { + __override__: false, + relationshipFieldName: lowerFirst(relationshipName), + relationshipFieldNamePlural: ({ relationshipFieldName }) => pluralize(relationshipFieldName), + relationshipNamePlural: pluralize(relationshipName), + relationshipNameCapitalized: upperFirst(relationshipName), + relationshipNameHumanized: startCase(relationshipName), + + propertyName: ({ relationshipFieldName, relationshipFieldNamePlural }) => + collection ? relationshipFieldNamePlural : relationshipFieldName, + }); + + prepareProperty(relationship); + mutateData(relationship, { + __override__: false, + // Other entity properties + otherEntityNamePlural: pluralize(otherEntityName), + otherEntityNameCapitalized: upperFirst(otherEntityName), + // let ownerSide true when type is 'many-to-one' for convenience. // means that this side should control the reference. - ownerSide: ({ ownerSide, relationshipLeftSide, relationshipManyToOne, relationshipOneToMany }) => - ownerSide ?? (relationshipManyToOne || (relationshipLeftSide && !relationshipOneToMany)), - persistableRelationship: ({ persistableRelationship, ownerSide }) => persistableRelationship ?? ownerSide, - relationshipUpdateBackReference: ({ relationshipUpdateBackReference, ownerSide, relationshipRightSide }) => - relationshipUpdateBackReference ?? (entityWithConfig.databaseType === 'neo4j' ? relationshipRightSide : !ownerSide), + ownerSide: relationshipManyToOne || (relationshipLeftSide && !relationshipOneToMany), + persistableRelationship: ({ ownerSide }) => ownerSide, + relationshipUpdateBackReference: ({ ownerSide, relationshipRightSide }) => + entityWithConfig.databaseType === 'neo4j' ? relationshipRightSide : !ownerSide, + + // DB properties + columnName: hibernateSnakeCase(relationshipName), + columnNamePrefix: relationship.id && relationshipOneToOne ? '' : `${hibernateSnakeCase(relationshipName)}_`, + shouldWriteJoinTable: ({ ownerSide }) => entityWithConfig.databaseType === 'sql' && relationshipManyToMany && ownerSide, + joinTable: ({ shouldWriteJoinTable }) => + shouldWriteJoinTable + ? { + name: getJoinTableName(entityWithConfig.entityTableName, relationshipName, { + prodDatabaseType: entityWithConfig.prodDatabaseType, + }).value, + } + : undefined, }); relationship.otherSideReferenceExists = false; @@ -78,33 +114,24 @@ export default function prepareRelationship(entityWithConfig, relationship, gene relationship.otherEntityIsEmbedded = otherEntityData.embedded; // Look for fields at the other other side of the relationship - if (otherEntityData.relationships) { - const otherRelationship = relationship.otherRelationship; - if (otherRelationship) { - relationship.otherSideReferenceExists = true; - mutateData(relationship, { - otherRelationship, - otherEntityRelationshipName: otherRelationship.relationshipName, - otherEntityRelationshipNamePlural: otherRelationship.relationshipNamePlural, - otherEntityRelationshipNameCapitalized: otherRelationship.relationshipNameCapitalized, - otherEntityRelationshipNameCapitalizedPlural: relationship.relationshipNameCapitalizedPlural, - }); - } else if ( - !ignoreMissingRequiredRelationship && - !relationship.relationshipIgnoreBackReference && - entityWithConfig.databaseType !== NEO4J && - entityWithConfig.databaseType !== DATABASE_NO && - (relationship.relationshipType === 'one-to-many' || relationship.ownerSide === false) - ) { - if (otherEntityData.builtInUser) { - throw new Error(`Error at entity ${entityName}: relationships with built-in User cannot have back reference`); - } - throw new Error( - `Error at entity ${entityName}: could not find the other side of the relationship ${stringifyApplicationData(relationship)}`, - ); - } else { - generator.debug(`Entity ${entityName}: Could not find the other side of the relationship ${stringifyApplicationData(relationship)}`); + const otherRelationship = relationship.otherRelationship; + if (otherRelationship) { + relationship.otherSideReferenceExists = true; + } else if ( + !ignoreMissingRequiredRelationship && + !relationship.relationshipIgnoreBackReference && + entityWithConfig.databaseType !== NEO4J && + entityWithConfig.databaseType !== DATABASE_NO && + (relationshipOneToMany || !relationship.ownerSide) + ) { + if (otherEntityData.builtInUser) { + throw new Error(`Error at entity ${entityName}: relationships with built-in User cannot have back reference`); } + throw new Error( + `Error at entity ${entityName}: could not find the other side of the relationship ${stringifyApplicationData(relationship)}`, + ); + } else { + generator.debug(`Entity ${entityName}: Could not find the other side of the relationship ${stringifyApplicationData(relationship)}`); } relationship.relatedField = otherEntityData.fields.find(field => field.fieldName === relationship.otherEntityField); @@ -122,30 +149,19 @@ export default function prepareRelationship(entityWithConfig, relationship, gene relationship.otherEntityFieldCapitalized = upperFirst(relationship.otherEntityField); } - if (relationship.otherEntityRelationshipName !== undefined) { - mutateData(relationship, { - otherEntityRelationshipNamePlural: pluralize(relationship.otherEntityRelationshipName), - otherEntityRelationshipNameCapitalized: upperFirst(relationship.otherEntityRelationshipName), - }); + if (relationship.otherEntityRelationshipName !== undefined || relationship.otherRelationship) { + // TODO remove at v9. mutateData(relationship, { - otherEntityRelationshipNameCapitalizedPlural: pluralize(relationship.otherEntityRelationshipNameCapitalized), + otherEntityRelationshipName: ({ otherRelationship, otherEntityRelationshipName }) => + lowerFirst(otherRelationship?.relationshipName ?? otherEntityRelationshipName), + otherEntityRelationshipNamePlural: ({ otherEntityRelationshipName }) => pluralize(otherEntityRelationshipName), + otherEntityRelationshipNameCapitalized: ({ otherEntityRelationshipName }) => upperFirst(otherEntityRelationshipName), + otherEntityRelationshipNameCapitalizedPlural: ({ otherEntityRelationshipNameCapitalized }) => + pluralize(otherEntityRelationshipNameCapitalized), }); } - const relationshipName = relationship.relationshipName; mutateData(relationship, { - relationshipNamePlural: pluralize(relationshipName), - relationshipFieldName: lowerFirst(relationshipName), - relationshipNameCapitalized: upperFirst(relationshipName), - relationshipNameHumanized: startCase(relationshipName), - columnName: hibernateSnakeCase(relationshipName), - columnNamePrefix: relationship.id && relationship.relationshipType === 'one-to-one' ? '' : `${hibernateSnakeCase(relationshipName)}_`, - otherEntityNamePlural: pluralize(otherEntityName), - otherEntityNameCapitalized: upperFirst(otherEntityName), - }); - - mutateData(relationship, { - relationshipFieldNamePlural: pluralize(relationship.relationshipFieldName), relationshipNameCapitalizedPlural: relationship.relationshipName.length > 1 ? pluralize(relationship.relationshipNameCapitalized) @@ -153,13 +169,6 @@ export default function prepareRelationship(entityWithConfig, relationship, gene otherEntityNameCapitalizedPlural: pluralize(relationship.otherEntityNameCapitalized), }); - mutateData(relationship, { - __override__: false, - propertyName: relationship.collection ? relationship.relationshipFieldNamePlural : relationship.relationshipFieldName, - }); - - prepareProperty(relationship); - if (entityWithConfig.dto === MAPSTRUCT) { if (otherEntityData.dto !== MAPSTRUCT && !otherEntityData.builtInUser) { generator.log.warn( @@ -175,7 +184,7 @@ export default function prepareRelationship(entityWithConfig, relationship, gene otherEntityFileName: otherEntityData.entityFileName, otherEntityFolderName: otherEntityData.entityFileName, jpaMetamodelFiltering: otherEntityData.jpaMetamodelFiltering, - unique: relationship.id || (relationship.ownerSide && relationship.relationshipType === 'one-to-one'), + unique: relationship.id || (relationship.ownerSide && relationshipOneToOne), }); const otherEntityClientRootFolder = otherEntityData.clientRootFolder || otherEntityData.microserviceName || ''; @@ -211,15 +220,6 @@ export default function prepareRelationship(entityWithConfig, relationship, gene } relationship.nullable = !(relationship.relationshipValidate === true && relationship.relationshipRequired); - relationship.shouldWriteJoinTable = relationship.relationshipType === 'many-to-many' && relationship.ownerSide; - if (relationship.shouldWriteJoinTable) { - relationship.joinTable = { - name: getJoinTableName(entityWithConfig.entityTableName, relationship.relationshipName, { - prodDatabaseType: entityWithConfig.prodDatabaseType, - }).value, - }; - } - relationship.reference = relationshipToReference(entityWithConfig, relationship); _defineOnUpdateAndOnDelete(relationship, generator); diff --git a/generators/liquibase/generator.ts b/generators/liquibase/generator.ts index ef15c0827a7e..2bcc0ef9bdd5 100644 --- a/generators/liquibase/generator.ts +++ b/generators/liquibase/generator.ts @@ -186,6 +186,14 @@ export default class LiquibaseGenerator extends BaseEntityChangesGenerator { prepareField(entity, field, this); prepareFieldForLiquibase(entity, field); } + } + } + + for (const databaseChangelog of entityChanges) { + if (!databaseChangelog.newEntity) { + // Previous entities are not prepared using default jhipster priorities. + // Prepare them. + const { previousEntity: entity } = databaseChangelog; for (const relationship of entity.relationships ?? []) { prepareRelationship(entity, relationship, this, true); prepareRelationshipForLiquibase(entity, relationship); diff --git a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs index 3b46e4e2dfce..469458617d49 100644 --- a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs +++ b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs @@ -1241,7 +1241,7 @@ _%> <%_ } else if (!reactive) { _%> <%= persistInstance %>.set<%= relationship.relationshipNameCapitalized %>(<%= relationship.relationshipFieldName %>); <%_ if (!relationship.ownerSide) { _%> - <%= relationship.relationshipFieldName %>.set<%= relationship.otherEntityRelationshipNameCapitalized %>(<%= persistInstance %>); + <%= relationship.relationshipFieldName %>.set<%= relationship.otherRelationship.propertyJavaBeanName %>(<%= persistInstance %>); <%_ } _%> <%_ } else { _%> <%= relationship.otherEntity.primaryKey.type %> <%= relationship.relationshipFieldName %>Id = <%= relationship.relationshipFieldName %>.get<%= relationship.otherEntity.primaryKey.nameCapitalized %>(); diff --git a/generators/spring-data-relational/templates/src/main/java/_package_/_entityPackage_/domain/_persistClass_.java.jhi.jakarta_persistence.ejs b/generators/spring-data-relational/templates/src/main/java/_package_/_entityPackage_/domain/_persistClass_.java.jhi.jakarta_persistence.ejs index f9ace222ba9d..e5360b26c18e 100644 --- a/generators/spring-data-relational/templates/src/main/java/_package_/_entityPackage_/domain/_persistClass_.java.jhi.jakarta_persistence.ejs +++ b/generators/spring-data-relational/templates/src/main/java/_package_/_entityPackage_/domain/_persistClass_.java.jhi.jakarta_persistence.ejs @@ -84,7 +84,7 @@ import org.hibernate.type.SqlTypes; @NotNull <%_ } _%> <%_ } else if (relationship.relationshipManyToMany) { -%> - @ManyToMany(fetch = FetchType.LAZY<% if (!relationship.ownerSide) { %>, mappedBy = "<%= relationship.otherEntityRelationshipNamePlural %>"<% } %>) + @ManyToMany(fetch = FetchType.LAZY<% if (!relationship.ownerSide) { %>, mappedBy = "<%= relationship.otherRelationship.propertyName %>"<% } %>) <%_ if (relationship.ownerSide) { _%> <%_ if (relationship.relationshipValidate && relationship.relationshipRequired) { _%> @NotNull