Skip to content

Commit 0d8fbd9

Browse files
committed
Load persisted entity again if there are readonly fields
to return the actual state of the entity and not something set from user side. May break tests now.
1 parent 6fe742d commit 0d8fbd9

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

generators/base-application/support/prepare-entity.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ function preparePostEntityCommonDerivedPropertiesNotTyped(entity: any) {
599599
entity.fields.some(field => !field.id && !field.transient) ||
600600
entity.relationships.some(relationship => !relationship.id && relationship.persistableRelationship);
601601

602+
entity.hasAnyReadonlyField = entity.fields.some(field => field.readonly && !(field.id && field.autoGenerate));
603+
602604
entity.allReferences
603605
.filter(reference => reference.relationship?.relatedField)
604606
.forEach(reference => {

generators/bootstrap-application/generator.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ describe(`generator - ${generator}`, () => {
10151015
"fieldsContainNoOwnerOneToOne": false,
10161016
"fluentMethods": true,
10171017
"generateFakeData": Any<Function>,
1018+
"hasAnyReadonlyField": false,
10181019
"hasCyclicRequiredRelationship": false,
10191020
"hasImageField": true,
10201021
"hasRelationshipWithBuiltInUser": false,
@@ -1347,6 +1348,7 @@ describe(`generator - ${generator}`, () => {
13471348
"fieldsContainNoOwnerOneToOne": false,
13481349
"fluentMethods": true,
13491350
"generateFakeData": Any<Function>,
1351+
"hasAnyReadonlyField": false,
13501352
"hasCyclicRequiredRelationship": false,
13511353
"hasRelationshipWithBuiltInUser": false,
13521354
"i18nAlertHeaderPrefix": "jhipsterApp.entityA",
@@ -1726,6 +1728,7 @@ describe(`generator - ${generator}`, () => {
17261728
"fieldsContainNoOwnerOneToOne": false,
17271729
"fluentMethods": true,
17281730
"generateFakeData": Any<Function>,
1731+
"hasAnyReadonlyField": false,
17291732
"hasCyclicRequiredRelationship": false,
17301733
"hasRelationshipWithBuiltInUser": false,
17311734
"i18nAlertHeaderPrefix": "jhipsterApp.entityA",

generators/spring-boot/templates/_global_partials_entity_/save_template.ejs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
<%- mapsIdAssoc.otherEntity.primaryKey.type %> <%- mapsIdAssoc.otherEntityName %>Id = <%- persistInstance %>.get<%- mapsIdAssoc.relationshipNameCapitalized %>().get<%- mapsIdAssoc.otherEntity.primaryKey.nameCapitalized %>();
2727
<%- mapsIdAssoc.otherEntity.entityInstance %>Repository.findById(<%- mapsIdAssoc.otherEntityName %>Id).ifPresent(<%- persistInstance %>::<%_ if (!fluentMethods) { _%>set<%- mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%- mapsIdAssoc.relationshipName %><%_ } _%>);
2828
<%_ } _%>
29-
<%- returnDirectly && !searchEngineElasticsearch && !dtoMapstruct ? 'return' : `${persistInstance} =` %> <%- entityInstance %>Repository.save(<%- persistInstance %>);
29+
<%- returnDirectly && !hasAnyReadonlyField && !searchEngineElasticsearch && !dtoMapstruct ? 'return' : `${persistInstance} =` %> <%- entityInstance %>Repository.save<% if (hasAnyReadonlyField && databaseType === 'sql') { %>AndFlush<% } %>(<%- persistInstance %>);
30+
<%_ if (hasAnyReadonlyField) { _%>
31+
<%- returnDirectly && !searchEngineElasticsearch && !dtoMapstruct ? 'return' : `${persistInstance} =` %> <%- entityInstance %>Repository.find<% if (implementsEagerLoadApis) { %>OneWithEagerRelationships<% } else { %>ById<% } %>(<%- persistInstance %>.get<%= primaryKey.nameCapitalized %>()).orElseThrow();
32+
<%_ } _%>
3033
<%_ if (searchEngineElasticsearch) { _%>
3134
<%- entityInstance %>SearchRepository.index(<%- persistInstance %>);
3235
<%_ } _%>

generators/spring-boot/templates/src/main/java/_package_/_entityPackage_/_partials_entity_/patch_template.ejs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ _%>
5151
return existing<%= entityClass %>;
5252
})
5353
<%_ } %>
54-
.<%= mapOrFlatMap %>(<%= entityInstance %>Repository::save)
54+
.<%= mapOrFlatMap %>(<%= entityInstance %>Repository::save<% if (hasAnyReadonlyField && databaseType === 'sql') { %>AndFlush<% } %>)
55+
<%_ if (hasAnyReadonlyField) { _%>
56+
.<%= mapOrFlatMap %>((persistedInstance) -> <%= entityInstance %>Repository.find<% if (implementsEagerLoadApis) { %>OneWithEagerRelationships<% } else { %>ById<% } %>(persistedInstance.get<%= primaryKey.nameCapitalized %>()).orElseThrow())
57+
<%_ } _%>
5558
<%_ if (searchEngineElasticsearch) { _%>
5659
.<%= mapOrFlatMap %>(saved<%= entityClass %> -> {
5760
<%_ if(reactive) { %>

0 commit comments

Comments
 (0)