0

I'm using ModelMapper to map UserDto class to User entity (details below) but it erroneously maps username to userId. How do I prevent this from happening.

public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long userId;
    private String username;
    ...

    // setters and getters
}

public class UserDto {
    private String username;
    ...

    // setters and getters
}

I have tried using

modelMapper.addMappings(mapper -> mapper.skip(User::setUserId));

but i get uncompilable source code when I run it.

Olantobi
  • 869
  • 1
  • 8
  • 16
  • 1
    It is not the default behavior. You should not need to skip the field. Do you use a loose strategy for the field matching? – davidxxx Jun 16 '17 at 18:57
  • Is there a variable called `serUserId` in your User class? Doesn't look like it from the code you've posted. – ManoDestra Jun 16 '17 at 18:57
  • "but i get uncompilable source code when I run it." Do you compile in Java 8 ? Method references (`User::serUserId`) require Java 8. – davidxxx Jun 16 '17 at 18:59
  • @davidxxx, I didn't set any matching strategy, so it should use the Standard strategy and i'm using Java 8. – Olantobi Jun 16 '17 at 20:42

0 Answers0