# Entity zu Dto Mapping mit Mapstruct liefert null



## Avalon (3. Jan 2021)

Hallo, wie muss in meinem Beispiel der Mapper aussehen. Ich bekomm immer null in den Dtos? 
[CODE lang="json" title="mit Mapstruct"]{"id":null,
"firstname":null,
"lastname":null,
"username":null,
"password":null,
"roles":null}[/CODE]
[CODE lang="json" title="ohne Mapstruct"]{
"id": 1,
"firstname": "Hans",
"lastname": "Dampf",
"username": "Avalon",
"password": "1234",
"roles": [
    {
        "id": 1,
        "name": "Administrator"
    },
    {
        "id": 2,
        "name": "User"
    }
    ]
}[/CODE]

[CODE lang="java" title="User.java"]
@Entity
@Table(name = "user")
@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String firstname;
    private String lastname;
    private String username;
    private String password;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles = new HashSet<>();

}[/CODE]
[CODE lang="java" title="Role.java"]
@Entity
@Table(name = "role")
@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class Role implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @JsonIgnore
    @ManyToMany(mappedBy = "roles")
    private Set<User> users = new HashSet<User>();
}[/CODE]
[CODE lang="java" title="UserDto.java"]@Getter @Setter
public class UserDto {

    private Long id;
    private String firstname;
    private String lastname;
    private String username;
    private String password;
    private Set<RoleDto> roles;

}[/CODE]
[CODE lang="java" title="RoleDto.java"]@Getter @Setter
public class RoleDto {

    private Long id;
    private String name;
    private Set<UserDto> users;
}[/CODE]
[CODE lang="java" title="IUserMapper.java"]@Mapper(componentModel = "spring")
public interface IUserMapper {

    UserDto mapToDto(User user);
}
[/CODE]
[CODE lang="java" title="IRoleMapper.java"]@Mapper(componentModel = "spring")
public interface IRoleMapper {

    RoleDto mapToDto(Role role);
}
[/CODE]


----------



## mrBrown (3. Jan 2021)

Wenn deine DTOs völlig identisch zu deinen Entitys sind: Lass sie weg.


Guck dir ansonsten mal die generierten Klassen an, ob die korrekt aussehen.


----------



## Avalon (3. Jan 2021)

mrBrown hat gesagt.:


> Guck dir ansonsten mal die generierten Klassen an, ob die korrekt aussehen.



Guter Tip. Die sehen irgendwie leer aus :-D


```
@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-01-03T12:47:19+0100",
    comments = "version: 1.4.1.Final, compiler: javac, environment: Java 1.8.0_241 (Oracle Corporation)"
)
@Component
public class IUserMapperImpl implements IUserMapper {

    @Override
    public UserDto mapToDto(User user) {
        if ( user == null ) {
            return null;
        }

        UserDto userDto = new UserDto();

        return userDto;
    }
}
```


----------



## mrBrown (3. Jan 2021)

Da du Lombok nutzt: https://mapstruct.org/faq/#can-i-use-mapstruct-together-with-project-lombok


----------



## Avalon (3. Jan 2021)

Danke. Nach 
	
	
	
	





```
mvn clean install
```
 sieht es aus wie oben. Erst wenn ich direkt danach das Projekt update, sieht es so aus.

```
@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-01-03T13:14:34+0100",
    comments = "version: 1.4.1.Final, compiler: Eclipse JDT (IDE) 1.3.1200.v20200916-0645, environment: Java 15.0.1 (Oracle Corporation)"
)
@Component
public class IUserMapperImpl implements IUserMapper {

    @Override
    public UserDto mapToDto(User user) {
        if ( user == null ) {
            return null;
        }

        UserDto userDto = new UserDto();

        userDto.setFirstname( user.getFirstname() );
        userDto.setId( user.getId() );
        userDto.setLastname( user.getLastname() );
        userDto.setPassword( user.getPassword() );
        userDto.setRoles( roleSetToRoleDtoSet( user.getRoles() ) );
        userDto.setUsername( user.getUsername() );

        return userDto;
    }

    protected Set<UserDto> userSetToUserDtoSet(Set<User> set) {
        if ( set == null ) {
            return null;
        }

        Set<UserDto> set1 = new HashSet<UserDto>( Math.max( (int) ( set.size() / .75f ) + 1, 16 ) );
        for ( User user : set ) {
            set1.add( mapToDto( user ) );
        }

        return set1;
    }

    protected RoleDto roleToRoleDto(Role role) {
        if ( role == null ) {
            return null;
        }

        RoleDto roleDto = new RoleDto();

        roleDto.setId( role.getId() );
        roleDto.setName( role.getName() );
        roleDto.setUsers( userSetToUserDtoSet( role.getUsers() ) );

        return roleDto;
    }

    protected Set<RoleDto> roleSetToRoleDtoSet(Set<Role> set) {
        if ( set == null ) {
            return null;
        }

        Set<RoleDto> set1 = new HashSet<RoleDto>( Math.max( (int) ( set.size() / .75f ) + 1, 16 ) );
        for ( Role role : set ) {
            set1.add( roleToRoleDto( role ) );
        }

        return set1;
    }
}
```
 Aber irgendwie fühlt sich das nicht richtig an. Ich lese dann wohl nochmal. Danke


----------

