jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.54k stars 4.02k forks source link

Partial update with MapStruct removes child entities #20751

Closed clementgpro closed 1 year ago

clementgpro commented 1 year ago

I am unsure whether the issue is a result of a technical flaw or if it is a result of my own error.

I have a RaceDTO which contains a list of pilots and a name. When I call the partialUpdate method to update the name of the race, the list of drivers is being removed. This is because the DTO is initialized with new HashSet<>, so the list is not null and MapStruct maps it, causing the service to clear my list of drivers. This should not be the case, as I only want to update the name of the race.

RaceDTO.class

private Set<RelayDTO> relays = new HashSet<>();

RaceMapperImpl.class

if ( entity.getPilots() != null ) { 
    Set<Pilot> set1 = pilotDTOSetToPilotSet( dto.getPilots() );
    if ( set1 != null ) { // set1 isn't null because it was initialized with new HashSet<>
        entity.getPilots().clear();
        entity.getPilots().addAll( set1 );
    }
}

Steps to reproduce:

Call the partialUpdate method on the RaceDTO, only updating the name field Observe that the list of drivers has been cleared

Expected behavior:

The partialUpdate method should only update the name field, and leave the list of drivers unchanged.

Actual behavior:

The partialUpdate method clears the list of drivers in the RaceDTO.

Solution:

Removes the initialization on the DTO

gmarziou commented 1 year ago

@Clemzd So what is your proposal? Have you tried removing the new HashSet<>() in DTO?

For me, MapStruct is more convenient for the generator than for the app developer. Personally, in all my JHipster apps, I removed MapStruct because it just adds complexity to simple code that you can very easily implement manually.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for too long without any activity. Due to the moving nature of jhipster generated application, bugs can become invalid. If this issue still applies please comment otherwise it will be closed in 7 days