映射结构映射器不添加多对一列进行映射
Map struct mapper not adding Many to One column for mapping
对于我的 spring-boot 应用程序,我想将 DTO 列转换为实体对象,因为我正在使用 mapstruct 映射器 class。但它没有将我的 ManyToOne 列转换为实体对象。我该如何添加
下面是我的实体 class
@Entity(name = "favorite_rooms")
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Favorite {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_fav_id")
@SequenceGenerator(name = "seq_fav_id")
Long id;
@ManyToOne
@JoinColumn(name = "animal_id")
Animal animal;
@ManyToOne
@JoinColumn(name = "room_id")
@ToString.Exclude
@EqualsAndHashCode.Exclude
Room rooms;
}
下面是我的映射器生成的代码class
public List<Favorite> favoriteDtosToFavorite(Set<FavoriteDto> favoriteRooms) {
if (favoriteRooms == null) {
return null;
} else {
List<Favorite> list = new ArrayList(favoriteRooms.size());
Iterator var3 = favoriteRooms.iterator();
while(var3.hasNext()) {
FavoriteDto favoriteDto = (FavoriteDto)var3.next();
list.add(this.favoriteDtoToFavorite(favoriteDto));
}
return list;
}
}
protected Favorite favoriteDtoToFavorite(FavoriteDto favoriteDto) {
if (favoriteDto == null) {
return null;
} else {
Favorite favorite = new Favorite();
favorite.setId(favoriteDto.getId());
return favorite;
}
}
正如我所见,它没有添加我的动物和房间数据
谁能告诉我如何添加它们?
指定每个子对象的映射方法。参见 documentation。
@Mapper
public interface FavoriteMapper {
FavoriteMapper INSTANCE = Mappers.getMapper(FavoriteMapper.class);
@Mapping(target = "id", source = "id")
Favorite mapFavorite(FavoriteDTO favoriteDTO);
@Mapping(target = "id", source = "id")
@Mapping(target = "name", source = "name")
Animal mapAnimal(AnimalDTO animalDTO);
@Mapping(target = "id", source = "id")
@Mapping(target = "name", source = "name")
Room mapRoom(RoomDTO animalDTO);
List<Favorite> favoriteDtosToFavorite(Set<FavoriteDTO> favoriteDTOSet);
}
生成的代码:
public class FavoriteMapperImpl implements FavoriteMapper {
@Override
public Favorite mapFavorite(FavoriteDTO favoriteDTO) {
if ( favoriteDTO == null ) {
return null;
}
Favorite favorite = new Favorite();
favorite.setId( favoriteDTO.getId() );
favorite.setAnimal( mapAnimal( favoriteDTO.getAnimal() ) );
favorite.setRooms( mapRoom( favoriteDTO.getRooms() ) );
return favorite;
}
@Override
public Animal mapAnimal(AnimalDTO animalDTO) {
if ( animalDTO == null ) {
return null;
}
Animal animal = new Animal();
animal.setId( animalDTO.getId() );
animal.setName( animalDTO.getName() );
return animal;
}
@Override
public Room mapRoom(RoomDTO animalDTO) {
if ( animalDTO == null ) {
return null;
}
Room room = new Room();
room.setId( animalDTO.getId() );
room.setName( animalDTO.getName() );
return room;
}
@Override
public List<Favorite> favoriteDtosToFavorite(Set<FavoriteDTO> favoriteDTOSet) {
if ( favoriteDTOSet == null ) {
return null;
}
List<Favorite> list = new ArrayList<Favorite>( favoriteDTOSet.size() );
for ( FavoriteDTO favoriteDTO : favoriteDTOSet ) {
list.add( mapFavorite( favoriteDTO ) );
}
return list;
}
}
使用的 DTO:
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class FavoriteDTO {
Long id;
AnimalDTO animal;
RoomDTO rooms;
}
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class AnimalDTO {
Long id;
String name;
}
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class RoomDTO {
Long id;
String name;
}
使用的实体:
@Entity(name = "favorite_rooms")
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Favorite {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_fav_id")
@SequenceGenerator(name = "seq_fav_id")
Long id;
@ManyToOne
@JoinColumn(name = "animal_id")
Animal animal;
@ManyToOne
@JoinColumn(name = "room_id")
@ToString.Exclude
@EqualsAndHashCode.Exclude
Room rooms;
}
@Entity(name = "favorite_animal")
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Animal {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_fav_id")
@SequenceGenerator(name = "seq_fav_id")
Long id;
String name;
}
@Entity(name = "favorite_room")
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Room {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_fav_id")
@SequenceGenerator(name = "seq_fav_id")
Long id;
String name;
}
对于我的 spring-boot 应用程序,我想将 DTO 列转换为实体对象,因为我正在使用 mapstruct 映射器 class。但它没有将我的 ManyToOne 列转换为实体对象。我该如何添加 下面是我的实体 class
@Entity(name = "favorite_rooms")
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Favorite {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_fav_id")
@SequenceGenerator(name = "seq_fav_id")
Long id;
@ManyToOne
@JoinColumn(name = "animal_id")
Animal animal;
@ManyToOne
@JoinColumn(name = "room_id")
@ToString.Exclude
@EqualsAndHashCode.Exclude
Room rooms;
}
下面是我的映射器生成的代码class
public List<Favorite> favoriteDtosToFavorite(Set<FavoriteDto> favoriteRooms) {
if (favoriteRooms == null) {
return null;
} else {
List<Favorite> list = new ArrayList(favoriteRooms.size());
Iterator var3 = favoriteRooms.iterator();
while(var3.hasNext()) {
FavoriteDto favoriteDto = (FavoriteDto)var3.next();
list.add(this.favoriteDtoToFavorite(favoriteDto));
}
return list;
}
}
protected Favorite favoriteDtoToFavorite(FavoriteDto favoriteDto) {
if (favoriteDto == null) {
return null;
} else {
Favorite favorite = new Favorite();
favorite.setId(favoriteDto.getId());
return favorite;
}
}
正如我所见,它没有添加我的动物和房间数据 谁能告诉我如何添加它们?
指定每个子对象的映射方法。参见 documentation。
@Mapper
public interface FavoriteMapper {
FavoriteMapper INSTANCE = Mappers.getMapper(FavoriteMapper.class);
@Mapping(target = "id", source = "id")
Favorite mapFavorite(FavoriteDTO favoriteDTO);
@Mapping(target = "id", source = "id")
@Mapping(target = "name", source = "name")
Animal mapAnimal(AnimalDTO animalDTO);
@Mapping(target = "id", source = "id")
@Mapping(target = "name", source = "name")
Room mapRoom(RoomDTO animalDTO);
List<Favorite> favoriteDtosToFavorite(Set<FavoriteDTO> favoriteDTOSet);
}
生成的代码:
public class FavoriteMapperImpl implements FavoriteMapper {
@Override
public Favorite mapFavorite(FavoriteDTO favoriteDTO) {
if ( favoriteDTO == null ) {
return null;
}
Favorite favorite = new Favorite();
favorite.setId( favoriteDTO.getId() );
favorite.setAnimal( mapAnimal( favoriteDTO.getAnimal() ) );
favorite.setRooms( mapRoom( favoriteDTO.getRooms() ) );
return favorite;
}
@Override
public Animal mapAnimal(AnimalDTO animalDTO) {
if ( animalDTO == null ) {
return null;
}
Animal animal = new Animal();
animal.setId( animalDTO.getId() );
animal.setName( animalDTO.getName() );
return animal;
}
@Override
public Room mapRoom(RoomDTO animalDTO) {
if ( animalDTO == null ) {
return null;
}
Room room = new Room();
room.setId( animalDTO.getId() );
room.setName( animalDTO.getName() );
return room;
}
@Override
public List<Favorite> favoriteDtosToFavorite(Set<FavoriteDTO> favoriteDTOSet) {
if ( favoriteDTOSet == null ) {
return null;
}
List<Favorite> list = new ArrayList<Favorite>( favoriteDTOSet.size() );
for ( FavoriteDTO favoriteDTO : favoriteDTOSet ) {
list.add( mapFavorite( favoriteDTO ) );
}
return list;
}
}
使用的 DTO:
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class FavoriteDTO {
Long id;
AnimalDTO animal;
RoomDTO rooms;
}
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class AnimalDTO {
Long id;
String name;
}
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class RoomDTO {
Long id;
String name;
}
使用的实体:
@Entity(name = "favorite_rooms")
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Favorite {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_fav_id")
@SequenceGenerator(name = "seq_fav_id")
Long id;
@ManyToOne
@JoinColumn(name = "animal_id")
Animal animal;
@ManyToOne
@JoinColumn(name = "room_id")
@ToString.Exclude
@EqualsAndHashCode.Exclude
Room rooms;
}
@Entity(name = "favorite_animal")
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Animal {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_fav_id")
@SequenceGenerator(name = "seq_fav_id")
Long id;
String name;
}
@Entity(name = "favorite_room")
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Room {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_fav_id")
@SequenceGenerator(name = "seq_fav_id")
Long id;
String name;
}