How to get a whole entity in Jhipster using mappers and DTOs?

Viewed 833

I'm new to mapstruct and I have a couple questions that I could not find an answer for.

I have an Entity1 (object) that has a Many2Many relationship with the Entity2. In this case, my Entity1DTO can bring all the data (Swagger) from Entity1 using a Set:

    private Set<Entity2DTO> appusers = new HashSet<>();

That Set has all the information about all the Entity2DTO objects.

Then, I have another Entity3 with a relation (One2Many) with Entity1. When I want to get all the same information with Entity3 (One2Many) it only brings the ID of Entity3 and the rest of the attributes are null.

    private Entity3DTO entity3;

Mapper:

@Mapping(target = "entity3", source = "entity3", qualifiedByName = "id")
Entity3DTO toDto(Entity3 s);

If I change the mapper and include, not only the id, but the other attributes (it will work), but I want to ask if there is an easier or more direct way to change the DTO or the mapper to bring the rest of the data, is there any?

And... Why it brings all data in the case of the Set, but not in the other case?

MapStruct documentation is not clear enough, so if you have any other examples with Jhipster that would be great.

Thanks for your help.

1 Answers

I'm not sure I have understood correctly, but I'm guessing your problem is that you want Entity1Mapper to map all the attributes of the nested Entity3 instead of just its id.

In your Entity1Mapper.java, change this:

@Mapping(target = "entity3", source = "entity3", qualifiedByName = "id")
Entity1DTO toDto(Entity1 s);

Into this:

@Mapping(target = "entity3", source = "entity3")
Entity1DTO toDto(Entity1 s);

This way you are telling MapStruct to use the full toDto(...) mapping method instead of toDtoId(...) that only fills the object ID.

Example project

I've generated a small JHipster 7.0.1 project to reproduce and fix your issue.

This is the JDL:

entity Entity1
entity Entity2
entity Entity3 { name String }

relationship ManyToMany { Entity1 to Entity2 }
relationship OneToMany { Entity3 to Entity1 }

dto all with mapstruct

With the freshly generated project you can start it up and go to the Entity1 main page and edit the first one to add an Entity3. If you inspect the GET call in the detail page for the Entity1 you just edited you will see that Entity3 only has an ID (notice how the name is NULL) as was expected.

{
  "id" : 1,
  "entity2s" : [ {
    "id" : 1
  } ],
  "entity3" : {
    "id" : 1,
    "name" : null
  }
}

Then go to Entity1Mapper.java and remove the , qualifiedByName = "id" part in the mapping of entity3. Like so:

Before:

    @Mapping(target = "entity2s", source = "entity2s", qualifiedByName = "idSet")
    @Mapping(target = "entity3", source = "entity3", qualifiedByName = "id")
    Entity1DTO toDto(Entity1 s);

After:

    @Mapping(target = "entity2s", source = "entity2s", qualifiedByName = "idSet")
    @Mapping(target = "entity3", source = "entity3")
    Entity1DTO toDto(Entity1 s);

Wait for the server side to recompile and reload the page, and there you have it:

{
  "id" : 1,
  "entity2s" : [ {
    "id" : 1
  } ],
  "entity3" : {
    "id" : 1,
    "name" : "Incredible Account"
  }
}
Related