Skip to content

Commit c9d5b7d

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 b0d0ee5 commit c9d5b7d

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

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

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

614+
entity.hasAnyReadonlyField = entity.fields.some(field => field.readonly && !(field.id && field.autoGenerate));
615+
614616
entity.allReferences
615617
.filter(reference => reference.relationship?.relatedField)
616618
.forEach(reference => {

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)