diff --git a/generators/base-application/types/entity.d.ts b/generators/base-application/types/entity.d.ts index 326c5beaf181..d30d16d74561 100644 --- a/generators/base-application/types/entity.d.ts +++ b/generators/base-application/types/entity.d.ts @@ -128,6 +128,11 @@ type Entity = Required & */ anyFieldHasFileBasedContentType: boolean; + /** + * Any relationship is required or id + */ + anyRelationshipIsRequired: boolean; + dtoMapstruct: boolean; }; diff --git a/generators/bootstrap-application-base/generator.ts b/generators/bootstrap-application-base/generator.ts index c1ced6a2c432..bbfa5917a902 100644 --- a/generators/bootstrap-application-base/generator.ts +++ b/generators/bootstrap-application-base/generator.ts @@ -382,6 +382,9 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator { get postPreparingEachEntity() { return this.asPostPreparingEachEntityTaskGroup({ + hasRequiredRelationship({ entity }) { + entity.anyRelationshipIsRequired = entity.relationships.some(rel => rel.relationshipRequired || rel.id); + }, checkForCircularRelationships({ entity }) { const detectCyclicRequiredRelationship = (entity, relatedEntities) => { if (relatedEntities.has(entity)) return true; 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 1ba83316dc5d..fecc57afbfe3 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 @@ -522,9 +522,9 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( * This is a static method, as tests for other entities might also need it, * if they test an entity which requires the current entity. */ - public static <%= persistClass %> create<% if (fieldStatus === 'UPDATED_') { _%>Updated<%_ } %>Entity(<% if (databaseTypeSql) { %>EntityManager em<% } %>) { + public static <%= persistClass %> create<% if (fieldStatus === 'UPDATED_') { _%>Updated<%_ } %>Entity(<% if (databaseTypeSql && anyRelationshipIsRequired) { %>EntityManager em<% } %>) { <%_ if (fluentMethods) { _%> - <% if (persistableRelationships.length === 0) { %>return <% } else { %><%= persistClass %> <%= persistInstance %> = <% } %>new <%= persistClass %>() + <% if (!anyRelationshipIsRequired) { %>return <% } else { %><%= persistClass %> <%= persistInstance %> = <% } %>new <%= persistClass %>() <%_ if (reactive && databaseTypeSql && primaryKey.typeUUID && !isUsingMapsId) { _%> .<%= primaryKey.name %>(UUID.randomUUID()) <%_ } _%> @@ -537,7 +537,7 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( .<%= field.fieldName %>ContentType(<%= fieldStatus + field.fieldNameUnderscored.toUpperCase() %>_CONTENT_TYPE) <%_ } _%> <%_ } _%>; - <%_ if (persistableRelationships.length === 0) { _%> + <%_ if (!anyRelationshipIsRequired) { _%> } <%_ return; _%> <%_ } _%> @@ -559,6 +559,7 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( <%_ const alreadyGeneratedEntities = []; for (relationship of persistableRelationships) { + const otherEntity = relationship.otherEntity; const relationshipValidate = relationship.relationshipValidate; const otherEntityName = relationship.otherEntityName; const otherEntityNameCapitalized = relationship.otherEntityNameCapitalized; @@ -569,7 +570,7 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( // Add required entity <%_ if (alreadyGeneratedEntities.indexOf(otherEntityName) == -1) { _%> <%_ if (relationship.otherEntityUser) { /* TODO or other entity has no unique fields */ _%> - <%= relationship.otherEntity.persistClass %> <%= otherEntityName %> = <%= createEntityPrefix %><%= otherEntityNameCapitalized %>ResourceIT.createEntity(<% if (databaseTypeSql) { %>em<% } %>)<%= createEntityPostfix %>; + <%= relationship.otherEntity.persistClass %> <%= otherEntityName %> = <%= createEntityPrefix %><%= otherEntityNameCapitalized %>ResourceIT.createEntity(<% if (databaseTypeSql && otherEntity.anyRelationshipIsRequired) { %>em<% } %>)<%= createEntityPostfix %>; <%_ if (databaseTypeSql && !reactive) { _%> em.persist(<%= otherEntityName %>); em.flush(); @@ -592,7 +593,7 @@ filterTestableRelationships.filter(rel => !rel.otherEntity.builtInUser).forEach( } <%_ } _%> <%_ } else { _%> - <%= otherEntityName %> = <%= createEntityPrefix %><%= otherEntityNameCapitalized %>ResourceIT.create<% if (fieldStatus === 'UPDATED_') { %>Updated<% } %>Entity(<% if (databaseType === 'sql') { %>em<% } %>)<%= createEntityPostfix %>; + <%= otherEntityName %> = <%= createEntityPrefix %><%= otherEntityNameCapitalized %>ResourceIT.create<% if (fieldStatus === 'UPDATED_') { %>Updated<% } %>Entity(<% if (databaseType === 'sql' && otherEntity.anyRelationshipIsRequired) { %>em<% } %>)<%= createEntityPostfix %>; <%_ } _%> <%_ if (databaseTypeMongodb) { _%> <%= otherEntityName %>.set<%= primaryKey.nameCapitalized %>("fixed-id-for-tests"); @@ -643,7 +644,7 @@ _%> <%_ } _%> @BeforeEach public void initTest() { - <%= persistInstance %> = createEntity(<% if (databaseTypeSql) { %>em<% } %>); + <%= persistInstance %> = createEntity(<% if (databaseTypeSql && anyRelationshipIsRequired) { %>em<% } %>); } @AfterEach @@ -796,13 +797,14 @@ _%> int searchDatabaseSizeBefore = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>); <%_ } _%> <%_ for (relationship of relationships) { + const otherEntity = relationship.otherEntity; const otherEntityName = relationship.otherEntityName; const otherEntityNameCapitalized = relationship.otherEntityNameCapitalized; const mapsIdUse = relationship.id; if (mapsIdUse) { _%> // Add a new parent entity <%_ if (alreadyGeneratedEntities.indexOf(otherEntityName) == -1) { _%> - <%= relationship.otherEntity.persistClass %> <%= otherEntityName %> = <%= otherEntityNameCapitalized %>ResourceIT.create<% if (!relationship.otherEntityUser) { _%>Updated<%_ } %>Entity(<% if (databaseTypeSql) { %>em<% } %>); + <%= relationship.otherEntity.persistClass %> <%= otherEntityName %> = <%= otherEntityNameCapitalized %>ResourceIT.create<% if (!relationship.otherEntityUser) { _%>Updated<%_ } %>Entity(<% if (databaseTypeSql && otherEntity.anyRelationshipIsRequired) { %>em<% } %>); <%_ if (databaseTypeSql && !reactive) { _%> em.persist(<%= otherEntityName %>); em.flush();