Skip to content

Commit

Permalink
Load persisted entity again if there are readonly fields
Browse files Browse the repository at this point in the history
to return the actual state of the entity and not something set from user side. May break tests now.
  • Loading branch information
OmarHawk committed Feb 18, 2025
1 parent b0d0ee5 commit cea1429
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions generators/base-application/support/prepare-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ function preparePostEntityCommonDerivedPropertiesNotTyped(entity: any) {
entity.fields.some(field => !field.id && !field.transient) ||
entity.relationships.some(relationship => !relationship.id && relationship.persistableRelationship);

entity.hasAnyReadonlyField = entity.fields.some(field => field.readonly);

entity.allReferences
.filter(reference => reference.relationship?.relatedField)
.forEach(reference => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
<%- mapsIdAssoc.otherEntity.primaryKey.type %> <%- mapsIdAssoc.otherEntityName %>Id = <%- persistInstance %>.get<%- mapsIdAssoc.relationshipNameCapitalized %>().get<%- mapsIdAssoc.otherEntity.primaryKey.nameCapitalized %>();
<%- mapsIdAssoc.otherEntity.entityInstance %>Repository.findById(<%- mapsIdAssoc.otherEntityName %>Id).ifPresent(<%- persistInstance %>::<%_ if (!fluentMethods) { _%>set<%- mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%- mapsIdAssoc.relationshipName %><%_ } _%>);
<%_ } _%>
<%- returnDirectly && !searchEngineElasticsearch && !dtoMapstruct ? 'return' : `${persistInstance} =` %> <%- entityInstance %>Repository.save(<%- persistInstance %>);
<%- returnDirectly && !hasAnyReadonlyField && !searchEngineElasticsearch && !dtoMapstruct ? 'return' : `${persistInstance} =` %> <%- entityInstance %>Repository.save<% if (hasAnyReadonlyField && databaseType === 'sql') { %>AndFlush<% } %>(<%- persistInstance %>);
<%_ if (hasAnyReadonlyField) { _%>
<%- returnDirectly && !searchEngineElasticsearch && !dtoMapstruct ? 'return' : `${persistInstance} =` %> <%- entityInstance %>Repository.find<% if (implementsEagerLoadApis) { %>OneWithEagerRelationships<% } else { %>ById<% } %>(<%- persistInstance %>.get<%= primaryKey.nameCapitalized %>()).orElseThrow();
<%_ } _%>
<%_ if (searchEngineElasticsearch) { _%>
<%- entityInstance %>SearchRepository.index(<%- persistInstance %>);
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ _%>
return existing<%= entityClass %>;
})
<%_ } %>
.<%= mapOrFlatMap %>(<%= entityInstance %>Repository::save)
.<%= mapOrFlatMap %>(<%= entityInstance %>Repository::save<% if (hasAnyReadonlyField && databaseType === 'sql') { %>AndFlush<% } %>)
<%_ if (hasAnyReadonlyField) { _%>
.<%= mapOrFlatMap %>((persistedInstance) -> <%= entityInstance %>Repository.find<% if (implementsEagerLoadApis) { %>OneWithEagerRelationships<% } else { %>ById<% } %>(persistedInstance.get<%= primaryKey.nameCapitalized %>()).orElseThrow())
<%_ } _%>
<%_ if (searchEngineElasticsearch) { _%>
.<%= mapOrFlatMap %>(saved<%= entityClass %> -> {
<%_ if(reactive) { %>
Expand Down

0 comments on commit cea1429

Please sign in to comment.