thearrow
thearrow

Reputation: 39

java- dto to model convert Error

i have dto and model layer.And i want convert dto to model layer like this code.How do i fix this error?I have all getter and setter i need

model/travel class

    public Travel convert(TravelDTO dto) {
    this.setTravelID(dto.getTravelID());
    this.setTravelCost(dto.getTravelCost());
    this.setTravelStart(dto.getTravelStart());
    this.setTravelEnd(dto.getTravelEnd());
    this.setLocation(dto.getLocation());
    this.setTravelPurpose(dto.getTravelPurpose());
    this.setUser(new User().convert(dto.getUser()));
    return this;
}

dto/travelDTO class

    public TravelDTO convert(Travel entity) {
    this.setTravelID(entity.getTravelID());
    this.setTravelCost(entity.getTravelCost());
    this.setTravelStart(entity.getTravelStart());
    this.setTravelEnd(entity.getTravelEnd());
    this.setLocation(entity.getLocation());
    this.setTravelPurpose(entity.getTravelPurpose());
    this.setUser(new UserDTO().convert(entity.getUser()));
    return this;
}

userDto / convert code

    public UserDTO convert(User entity) {
    this.setUserID(entity.getUserID());
    this.setFirstName(entity.getFirstName());
    this.setLastName(entity.getLastName());
    this.setManagerId(entity.getManagerId());
    this.setPassword(entity.getPassword());
    this.setRegNumber(entity.getRegNumber());
    this.setUserName(entity.getUserName());
    this.setDepartment(new DepartmentDTO().convert(entity.getDepartment()));
    this.setTravel(new TravelDTO().convert(entity.getTravel()));

    return this;
}

ERROR

Upvotes: 0

Views: 1235

Answers (3)

Vasily Vlasov
Vasily Vlasov

Reputation: 3346

As I can see from you screenshot, UserDTO.convert method accepts argument of type User, and you are trying to pass argument with type List. I guess, the possible solution is to make Travel.getUser() return User instead of List.

UPDATE

You may iterate through your list of Users, converting each one to UserDTO and then adding it to collection, then passing it as an argument to this.setUser. Something like this: `

List<UserDTO> userDTOs = new ArrayList<>();
List<User> users = entity.getUser();
for (User user : users) { 
    UserDTO userDTO = new UserDTO.convert(user);
    userDTOs.add(userDTO);
} 
this.setUser(userDTOs); 

And please pay attention that your TravelDTO class has List<User> user field. I guess, it should be List<UserDTO> users.

Upvotes: 0

rpfun12
rpfun12

Reputation: 89

The error message explains the issue :). It says the entity.getUser() is returning a list of users. But the method accepts one User object.

Upvotes: 1

mhasan
mhasan

Reputation: 3709

The problem is your entity.getUser() is returning List whereas your convert method of UserDTO is expecting single User model object.

Upvotes: 0

Related Questions