Skip to content

Commit

Permalink
reorganize relationship preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 20, 2024
1 parent 5b3535c commit ceadea9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 78 deletions.
152 changes: 76 additions & 76 deletions generators/base-application/support/prepare-relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -52,59 +52,86 @@ 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;

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);
Expand All @@ -122,44 +149,26 @@ 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)
: upperFirst(pluralize(relationship.relationshipName)),
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(
Expand All @@ -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 || '';
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions generators/liquibase/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ceadea9

Please sign in to comment.