将对象转换为Long时使用mapstruct的未映射目标属性

问题描述 投票:0回答:1

在将对象转换为Long时,我在mapstruct上遇到问题,出现以下警告:

warning: Unmapped target property

这里是实体(使用龙目岛的人):

    @Getter
    @Setter
    @NoArgsConstructor
    @Entity
    public class User {
        ...
        private Set<Address> addresses= new HashSet<>();
        ...
    }

    @Getter
    @Setter
    @NoArgsConstructor
    @Entity
    public class Address {
        ...
        private Town town;
        ...
    }

    @Getter
    @Setter
    @NoArgsConstructor
    @Entity
    public class Town {
        ...
        private Long id;
        ...
    }

和DTO:

    @Getter
    @Setter
    @NoArgsConstructor
    public class UserDTO {
        ...
        private Set<AddressDTO> addresses= new HashSet<>();
        ...
    }

    @Getter
    @Setter
    @NoArgsConstructor
    public class AddressDTO {
        ...
        private Long townId;
        ...
    }

在addressDTO中,我需要townId而不是town对象。这是映射器:

@Mapper
public interface UserMapper {

    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    UserDTO userToUserDTO(User user);

}

@Mapper
public interface AddressMapper {

    AddressMapper INSTANCE = Mappers.getMapper(AddressMapper.class);

    @Mapping(target = "townId", source = "town")
    AddressDTO addressToAddressDTO(Address address);

    default Long getTownId(Town town) {
        return town.getId();
    }
}

我为AddressMapper编写了一个有效的单元测试:

AddressDTO addressDTO = AddressMapper.INSTANCE.addressToAddresslDTO( address);

但不适用于UserMapper:

UserDTO userDTO = userMapper.INSTANCE.userToUserDTO( user);

我有以下警告:

warning: Unmapped target property: "townId". Mapping from Collection element "fr.example.myproj.entity.Adress adresses" to "fr.example.myproj.service.dto.AdressDTO adresses".
    UserDTO userToUserDTO(User user);
java lombok mapstruct
1个回答
0
投票
@Mapper public interface UserMapper { UserMapper INSTANCE = Mappers.getMapper(UserMapper.class); UserDTO userToUserDTO(User user); default AddressDTO addressToAddressDTO(Address address) { return AddressMapper.INSTANCE.addressToAdressDTO( address ); } }
© www.soinside.com 2019 - 2024. All rights reserved.