From 9e06d2aae00c71a8107c6d295d626226119ba485 Mon Sep 17 00:00:00 2001 From: xetys Date: Tue, 14 Jan 2020 23:12:45 +0100 Subject: [PATCH] enables relationships to 'Authority' in entity-server and ensures using the correct fields in generated files fix #11062 --- .../main/java/package/domain/Entity.java.ejs | 8 +++++--- .../liquibase/changelog/added_entity.xml.ejs | 6 ++++-- .../changelog/added_entity_constraints.xml.ejs | 17 ++++++++++------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs b/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs index b28aa1ad43d3..6c94bb244a23 100644 --- a/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs +++ b/generators/entity-server/templates/src/main/java/package/domain/Entity.java.ejs @@ -257,6 +257,7 @@ public class <%= asEntity(entityClass) %> implements Serializable { const otherEntityNameCapitalized = relationships[idx].otherEntityNameCapitalized; const ownerSide = relationships[idx].ownerSide; const isUsingMapsId = relationships[idx].useJPADerivedIdentifier; + const idSuffix = relationshipName.toLowerCase() === 'authority' ? 'name' : 'id'; if (otherEntityRelationshipName) { mappedBy = otherEntityRelationshipName.charAt(0).toLowerCase() + otherEntityRelationshipName.slice(1) } @@ -329,7 +330,7 @@ public class <%= asEntity(entityClass) %> implements Serializable { <%_ } _%> @JoinTable(name = "<%= joinTableName %>", joinColumns = @JoinColumn(name = "<%= getColumnName(name) %>_id", referencedColumnName = "id"), - inverseJoinColumns = @JoinColumn(name = "<%= getColumnName(relationships[idx].relationshipName) %>_id", referencedColumnName = "id")) + inverseJoinColumns = @JoinColumn(name = "<%= getColumnName(relationships[idx].relationshipName) %>_<%= idSuffix %>", referencedColumnName = "<%= idSuffix %>")) <%_ } } else if (databaseType === 'mongodb' || databaseType === 'couchbase') { if (databaseType === 'mongodb') { _%> @@ -451,6 +452,7 @@ public class <%= asEntity(entityClass) %> implements Serializable { const otherEntityNamePlural = relationships[idx].otherEntityNamePlural; const otherEntityRelationshipNameCapitalized = relationships[idx].otherEntityRelationshipNameCapitalized; const otherEntityRelationshipNameCapitalizedPlural = relationships[idx].otherEntityRelationshipNameCapitalizedPlural; + const isOtherEntityFromUserManagement = asEntity(otherEntityNameCapitalized) === 'User' || asEntity(otherEntityNameCapitalized) === 'Authority'; _%> <%_ if (relationshipType === 'one-to-many' || relationshipType === 'many-to-many') { _%> @@ -476,7 +478,7 @@ public class <%= asEntity(entityClass) %> implements Serializable { <%_ } _%> <%_ if (relationshipType === 'one-to-many') { _%> <%= otherEntityName %>.set<%= otherEntityRelationshipNameCapitalized %>(this); - <%_ } else if (otherEntityRelationshipNameCapitalizedPlural !== '' && asEntity(otherEntityNameCapitalized)!=='User' && relationshipType === 'many-to-many') { + <%_ } else if (otherEntityRelationshipNameCapitalizedPlural !== '' && !isOtherEntityFromUserManagement && relationshipType === 'many-to-many') { // JHipster version < 3.6.0 didn't ask for this relationship name _%> <%= otherEntityName %>.get<%= otherEntityRelationshipNameCapitalizedPlural %>().add(this); <%_ } _%> @@ -490,7 +492,7 @@ public class <%= asEntity(entityClass) %> implements Serializable { <%_ } _%> <%_ if (relationshipType === 'one-to-many') { _%> <%= otherEntityName %>.set<%= otherEntityRelationshipNameCapitalized %>(null); - <%_ } else if (otherEntityRelationshipNameCapitalizedPlural !== '' && asEntity(otherEntityNameCapitalized)!=='User' && relationshipType === 'many-to-many') { + <%_ } else if (otherEntityRelationshipNameCapitalizedPlural !== '' && !isOtherEntityFromUserManagement && relationshipType === 'many-to-many') { // JHipster version < 3.6.0 didn't ask for this relationship name _%> <%= otherEntityName %>.get<%= otherEntityRelationshipNameCapitalizedPlural %>().remove(this); <%_ } _%> diff --git a/generators/entity-server/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs b/generators/entity-server/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs index db383a46bdd6..a7165223a9e2 100644 --- a/generators/entity-server/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs +++ b/generators/entity-server/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs @@ -162,10 +162,12 @@ otherEntityName = relationships[idx].otherEntityName; if (relationshipType === 'many-to-many' && ownerSide) { const joinTableName = getJoinTableName(entityTableName, relationshipName, prodDatabaseType); + const idSuffix = relationshipName.toLowerCase() === 'authority' ? 'name' : 'id'; + const idColumnType = relationshipName.toLowerCase() === 'authority' ? 'varchar(50)' : 'bigint'; _%> - + @@ -173,7 +175,7 @@ - + <% } %><% } %> diff --git a/generators/entity-server/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs b/generators/entity-server/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs index 5f3cc7be5076..66f019ce6a2f 100644 --- a/generators/entity-server/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs +++ b/generators/entity-server/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs @@ -28,24 +28,27 @@ <% for (idx in relationships) { const relationshipType = relationships[idx].relationshipType, relationshipName = relationships[idx].relationshipName, + idSuffix = relationshipName.toLowerCase() === 'authority' ? 'name' : 'id', ownerSide = relationships[idx].ownerSide, - otherEntityTableName = relationships[idx].otherEntityTableName; - if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide)) { + otherEntityTableName = relationshipName.toLowerCase() === 'authority' + ? 'jhi_' + relationships[idx].otherEntityTableName + : relationships[idx].otherEntityTableName; + if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide)) { const constraintName = getFKConstraintName(entityTableName, relationshipName, prodDatabaseType); - let baseColumnName = getColumnName(relationshipName) + '_id'; + let baseColumnName = getColumnName(relationshipName) + '_' + idSuffix; if (relationshipType === 'one-to-one' && ownerSide && relationships[idx].useJPADerivedIdentifier === true) { baseColumnName = 'id'; } %> <%_ } else if (relationshipType === 'many-to-many' && ownerSide) { const joinTableName = getJoinTableName(entityTableName, relationshipName, prodDatabaseType); const constraintName = getFKConstraintName(joinTableName, getColumnName(entityTableName), prodDatabaseType, true); const otherEntityConstraintName = getFKConstraintName(joinTableName, getColumnName(relationshipName), prodDatabaseType, true); - _%> + _%> - <% } %><% } %>