diff --git a/plugins/asyncapi-spring-cloud-streams3/src/main/java/io/zenwave360/sdk/plugins/SpringCloudStreams3Generator.java b/plugins/asyncapi-spring-cloud-streams3/src/main/java/io/zenwave360/sdk/plugins/SpringCloudStreams3Generator.java index c10e84d6..24f8c618 100644 --- a/plugins/asyncapi-spring-cloud-streams3/src/main/java/io/zenwave360/sdk/plugins/SpringCloudStreams3Generator.java +++ b/plugins/asyncapi-spring-cloud-streams3/src/main/java/io/zenwave360/sdk/plugins/SpringCloudStreams3Generator.java @@ -114,7 +114,7 @@ public enum TransactionalOutboxType { handlebarsEngine.getHandlebars().registerHelper("methodSuffix", (context, options) -> { boolean doExposeMessage = "true".equals(String.valueOf(options.hash.get("exposeMessage"))); boolean isProducer = "true".equals(String.valueOf(options.hash.get("producer")));; - if (isProducer || doExposeMessage || exposeMessage || style == ProgrammingStyle.reactive) { + if (doExposeMessage || exposeMessage || style == ProgrammingStyle.reactive) { int messagesCount = JSONPath.get(options.param(0), "$.x--messages.length()", 0); if (messagesCount > 1) { String messageJavaType = JSONPath.get(context, "$.x--javaTypeSimpleName"); diff --git a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/Entity.java.hbs b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/Entity.java.hbs index 4e5fdf83..59a92b5f 100644 --- a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/Entity.java.hbs +++ b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/Entity.java.hbs @@ -83,8 +83,8 @@ public {{abstractClass entity}} class {{entity.className}} {{addExtends entity}} @Column(name = "{{snakeCase relationship.fieldName}}_id") private {{idJavaType}} {{relationship.fieldName}}Id; {{~/if}} - {{> (partial 'partials/' relationship.type)}} {{#unless (addRelationshipById relationship entity=entity)}}{{#if relationship.required}}@NotNull{{/if}}{{/unless}} + {{> (partial 'partials/' relationship.type)}} private {{{relationshipFieldType relationship}}} {{relationship.fieldName}} {{{relationshipFieldTypeInitializer relationship}}}; {{/if~}} {{/each}} @@ -104,6 +104,38 @@ public {{abstractClass entity}} class {{entity.className}} {{addExtends entity}} protected LocalDateTime lastModifiedDate; {{~/if}} +{{~#each (jsonPath entity '$.relationships[*][?(@.ownerSide == true)]') as |relationship|}} + // manage relationships + {{~#if (eq relationship.type 'OneToOne')}} + public {{entity.className}} set{{capitalize relationship.fieldName}}({{{relationshipFieldType relationship}}} {{relationship.fieldName}}) { + this.{{relationship.fieldName}} = {{relationship.fieldName}}; + {{relationship.fieldName}}.set{{capitalize relationship.otherEntityFieldName}}(this); + return this; + } + {{~/if}} + {{~#if (eq relationship.type 'OneToMany')}} + public {{entity.className}} add{{capitalize relationship.fieldName}}({{relationship.otherEntityName}} {{relationship.fieldName}}) { + this.{{relationship.fieldName}}.add({{relationship.fieldName}}); + {{relationship.fieldName}}.set{{capitalize relationship.otherEntityFieldName}}(this); + return this; + } + {{~/if}} + {{~#if (eq relationship.type 'ManyToMany')}} + public {{entity.className}} add{{capitalize relationship.fieldName}}({{relationship.otherEntityName}} {{relationship.fieldName}}) { + this.{{relationship.fieldName}}.add({{relationship.fieldName}}); + {{relationship.fieldName}}.get{{capitalize relationship.otherEntityFieldName}}().add(this); + return this; + } + {{~/if}} + {{~#if (and (eq relationship.type 'ManyToOne') relationship.otherEntityFieldName)}} + public {{entity.className}} set{{capitalize relationship.fieldName}}({{{relationshipFieldType relationship}}} {{relationship.fieldName}}) { + this.{{relationship.fieldName}} = {{relationship.fieldName}}; + {{relationship.fieldName}}.get{{capitalize relationship.otherEntityFieldName}}().add(this); + return this; + } + {{~/if}} +{{/each}} + {{#unless useLombok~}} {{#unless (skipEntityId entity)~}} public {{idJavaType}} getId() { @@ -147,10 +179,12 @@ public {{abstractClass entity}} class {{entity.className}} {{addExtends entity}} public {{{relationshipFieldType relationship}}} get{{capitalize relationship.fieldName}}() { return {{relationship.fieldName}}; } + {{~#unless (and (endsWith relationship.type 'ToOne') relationship.ownerSide relationship.otherEntityFieldName)}} public {{entity.className}} set{{capitalize relationship.fieldName}}({{{relationshipFieldType relationship}}} {{relationship.fieldName}}) { this.{{relationship.fieldName}} = {{relationship.fieldName}}; return this; } + {{~/unless}} {{/if~}} {{/each}} @@ -191,7 +225,7 @@ public {{abstractClass entity}} class {{entity.className}} {{addExtends entity}} return this; } {{/if}} -{{/unless~}} +{{/unless~}}{{!-- ends no lombok --}} {{#unless (skipEntityId entity)~}} /* https://vladmihalcea.com/the-best-way-to-implement-equals-hashcode-and-tostring-with-jpa-and-hibernate/ */ @@ -204,7 +238,7 @@ public {{abstractClass entity}} class {{entity.className}} {{addExtends entity}} return false; } {{entity.className}} other = ({{entity.className}}) o; - return id != null && id.equals(other.id); + return getId() != null && getId().equals(other.getId()); } @Override diff --git a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/ManyToMany.hbs b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/ManyToMany.hbs index 0a2a882a..143795d2 100644 --- a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/ManyToMany.hbs +++ b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/ManyToMany.hbs @@ -1,8 +1,5 @@ @ManyToMany{{#if relationship.ownerSide}}(cascade = CascadeType.ALL){{else}}(mappedBy = "{{relationship.otherEntityFieldName}}"){{/if}} {{~#if relationship.ownerSide}} - {{~#if relationship.required}} -@NotNull - {{~/if}} @JoinTable(name = "{{snakeCase (concat entity.name relationship.otherEntityName)}}", joinColumns = @JoinColumn(name = "{{snakeCase (concat entity.name '_id')}}", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "{{snakeCase (concat relationship.otherEntityName '_id')}}", referencedColumnName = "id")) diff --git a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/ManyToOne.hbs b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/ManyToOne.hbs index 2cad0d19..940b7469 100644 --- a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/ManyToOne.hbs +++ b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/ManyToOne.hbs @@ -1,9 +1,6 @@ {{#if relationship.ownerSide}} -@ManyToOne({{#if (addRelationshipById relationship entity=entity)}}fetch = FetchType.LAZY{{else}}cascade = CascadeType.ALL{{/if}}) +@ManyToOne(fetch = FetchType.LAZY {{#unless (addRelationshipById relationship entity=entity)}}, cascade = CascadeType.ALL{{/unless}}) @JoinColumn(name = "{{snakeCase relationship.fieldName}}_id"{{#if (addRelationshipById relationship entity=entity)}}, updatable = false, insertable = false{{/if}}) {{else}} - {{~#if relationship.required}} -@NotNull - {{~/if}} @OneToMany(mappedBy="{{relationship.otherEntityFieldName}}", fetch = FetchType.LAZY) {{/if}} diff --git a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/OneToMany.hbs b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/OneToMany.hbs index bc66759c..94092a72 100644 --- a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/OneToMany.hbs +++ b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/OneToMany.hbs @@ -1,8 +1,5 @@ {{#if relationship.ownerSide}} - {{~#if relationship.required}} -@NotNull - {{~/if}} -@OneToMany(mappedBy = "{{relationship.otherEntityFieldName}}"{{#if entity.options.aggregate}}, cascade = CascadeType.ALL, orphanRemoval = true{{/if}}) +@OneToMany({{#if relationship.otherEntityFieldName}}mappedBy = "{{relationship.otherEntityFieldName}}",{{/if}} fetch = FetchType.LAZY{{#if entity.options.aggregate}}, cascade = CascadeType.ALL, orphanRemoval = true{{/if}}) {{else}} @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "{{snakeCase relationship.fieldName}}_id") diff --git a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/OneToOne.hbs b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/OneToOne.hbs index 055f5707..71aa1a2d 100644 --- a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/OneToOne.hbs +++ b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/jpa/partials/OneToOne.hbs @@ -1,15 +1,21 @@ -{{#if relationship.ownerSide }} -@OneToOne(fetch = FetchType.LAZY {{#if entity.options.aggregate}}, cascade = CascadeType.ALL, orphanRemoval = true{{/if}}) - {{~#if relationship.required}} -@NotNull - {{~/if}} +{{#if relationship.otherEntityFieldName ~}} + {{!-- bidirectional --}} + {{#if relationship.ownerSide }} + @OneToOne(mappedBy = "{{relationship.otherEntityFieldName}}", fetch = FetchType.LAZY {{#if entity.options.aggregate}}, cascade = CascadeType.ALL, orphanRemoval = true{{/if}}) + {{~else~}} + {{~#if relationship.mapsId}} + @MapsId + @JoinColumn(name = "id") + {{~else}} + @JoinColumn(unique = true) + {{/if~}} + @OneToOne(fetch = FetchType.LAZY {{#if entity.options.aggregate}}, cascade = CascadeType.ALL, orphanRemoval = true{{/if}}) + {{/if~}} {{~else~}} - {{~#if relationship.mapsId}} -@MapsId -@JoinColumn(name = "id") - {{~else}} -@JoinColumn(unique = true) + {{!-- unidirectional --}} + {{#if relationship.ownerSide }} + @JoinColumn(unique = true) + @OneToOne(fetch = FetchType.LAZY {{#if entity.options.aggregate}}, cascade = CascadeType.ALL, orphanRemoval = true{{/if}}) {{/if~}} -@OneToOne(mappedBy = "{{relationship.otherEntityFieldName}}", fetch = FetchType.LAZY {{#if entity.options.aggregate}}, cascade = CascadeType.ALL, orphanRemoval = true{{/if}}) -{{/if~}} +{{~/if~}} diff --git a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/mongodb/Entity.java.hbs b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/mongodb/Entity.java.hbs index a40a963e..c535b181 100644 --- a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/mongodb/Entity.java.hbs +++ b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/main/java/core/domain/mongodb/Entity.java.hbs @@ -123,7 +123,7 @@ public {{abstractClass entity}} class {{entity.className}} {{addExtends entity}} return false; } {{entity.className}} other = ({{entity.className}}) o; - return id != null && id.equals(other.id); + return getId() != null && getId().equals(other.getId()); } @Override diff --git a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/jpa/imperative/BaseRepositoryIntegrationTest.java.hbs b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/jpa/imperative/BaseRepositoryIntegrationTest.java.hbs index 14d173c5..8d0fc960 100644 --- a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/jpa/imperative/BaseRepositoryIntegrationTest.java.hbs +++ b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/jpa/imperative/BaseRepositoryIntegrationTest.java.hbs @@ -1,4 +1,4 @@ -package {{infrastructurePackage}}.jpa; +package {{infrastructureRepositoryPackage}}; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; diff --git a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/jpa/imperative/EntityRepositoryIntegrationTest.java.hbs b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/jpa/imperative/EntityRepositoryIntegrationTest.java.hbs index a8635df8..b31e16c5 100644 --- a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/jpa/imperative/EntityRepositoryIntegrationTest.java.hbs +++ b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/jpa/imperative/EntityRepositoryIntegrationTest.java.hbs @@ -1,4 +1,4 @@ -package {{infrastructurePackage}}.jpa; +package {{infrastructureRepositoryPackage}}; import java.util.HashSet; import java.time.*; @@ -75,7 +75,9 @@ public class {{entity.className}}RepositoryIntegrationTest extends BaseRepositor // Persist aggregate root var created = {{entity.instanceName}}Repository.save({{entity.instanceName}}); - entityManager.refresh(created); // reloading to get relationships persisted by id + // reloading to get relationships persisted by id + entityManager.flush(); + entityManager.refresh(created); Assertions.assertTrue(created.getId() != null); Assertions.assertTrue(created.getVersion() != null); {{~#if (or entity.options.auditing entity.options.extendsAuditing)}} diff --git a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/mongodb/imperative/BaseRepositoryIntegrationTest.java.hbs b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/mongodb/imperative/BaseRepositoryIntegrationTest.java.hbs index cfce3868..8d0fc960 100644 --- a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/mongodb/imperative/BaseRepositoryIntegrationTest.java.hbs +++ b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/mongodb/imperative/BaseRepositoryIntegrationTest.java.hbs @@ -1,4 +1,4 @@ -package {{infrastructurePackage}}.mongodb; +package {{infrastructureRepositoryPackage}}; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; diff --git a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/mongodb/imperative/EntityRepositoryIntegrationTest.java.hbs b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/mongodb/imperative/EntityRepositoryIntegrationTest.java.hbs index a990b7ae..2c5cbdf1 100644 --- a/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/mongodb/imperative/EntityRepositoryIntegrationTest.java.hbs +++ b/plugins/backend-application-default/src/main/resources/io/zenwave360/sdk/plugins/BackendApplicationDefaultGenerator/src/test/java/infrastructure/mongodb/imperative/EntityRepositoryIntegrationTest.java.hbs @@ -1,4 +1,4 @@ -package {{infrastructurePackage}}.mongodb; +package {{infrastructureRepositoryPackage}}; import {{entitiesPackage}}.*; import {{outboundRepositoryPackage}}.{{entity.className}}Repository;