Skip to content

Commit

Permalink
enables relationships to 'Authority' in entity-server
Browse files Browse the repository at this point in the history
and ensures using the correct fields in generated files

fix jhipster#11062
  • Loading branch information
xetys committed Jan 14, 2020
1 parent bbe4759 commit 9e06d2a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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') { _%>
Expand Down Expand Up @@ -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') { _%>

Expand All @@ -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);
<%_ } _%>
Expand All @@ -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);
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,20 @@
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';
_%>
<createTable tableName="<%= joinTableName %>">
<column name="<%= getColumnName(relationshipName) %>_id" type="bigint">
<column name="<%= getColumnName(relationshipName) %>_<%= idSuffix %>" type="<%= idColumnType %>">
<constraints nullable="false"/>
</column>
<column name="<%= getColumnName(name) %>_id" type="bigint">
<constraints nullable="false"/>
</column>
</createTable>
<addPrimaryKey columnNames="<%= getColumnName(name) %>_id, <%= getColumnName(relationshipName) %>_id" tableName="<%= joinTableName %>"/>
<addPrimaryKey columnNames="<%= getColumnName(name) %>_id, <%= getColumnName(relationshipName) %>_<%= idSuffix %>" tableName="<%= joinTableName %>"/>
<% } %><% } %>
</changeSet>
<!-- jhipster-needle-liquibase-add-changeset - JHipster will add changesets here, do not remove-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,38 @@
<% 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';
} %>
<addForeignKeyConstraint baseColumnNames="<%= baseColumnName %>"
baseTableName="<%= entityTableName %>"
constraintName="<%= constraintName %>"
referencedColumnNames="id"
referencedColumnNames="<%= idSuffix %>"
referencedTableName="<%= otherEntityTableName %>"/>
<%_ } 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);
_%>
_%>
<addForeignKeyConstraint baseColumnNames="<%= getColumnName(name) %>_id"
baseTableName="<%= joinTableName %>"
constraintName="<%= constraintName %>"
referencedColumnNames="id"
referencedTableName="<%= entityTableName %>"/>
<addForeignKeyConstraint baseColumnNames="<%= getColumnName(relationshipName) %>_id"
<addForeignKeyConstraint baseColumnNames="<%= getColumnName(relationshipName) %>_<%= idSuffix %>"
baseTableName="<%= joinTableName %>"
constraintName="<%= getColumnName(otherEntityConstraintName) %>"
referencedColumnNames="id"
referencedColumnNames="<%= idSuffix %>"
referencedTableName="<%= otherEntityTableName %>"/>
<% } %><% } %>
</changeSet>
Expand Down

0 comments on commit 9e06d2a

Please sign in to comment.