У меня есть структура слоя в проекте. (DTO, Buisness, Entity)
В этой настройке отображения я игнорирую пользователя /бронирования, чтобы они были «нулевыми». Если я установите его в разные значения, он попадает в бесконечный цикл (Stackoverflow)
мне нужно получить бронирование с информацией о пользователе (пользователь и т. Д.)
Код: Выделить всё
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface ReservationsEntityMapper {
@Mapping(target = "user", ignore = true)
ReservationsEntity mapToEntity(Reservations entity);
@Mapping(target = "user", ignore = true)
Reservations mapFromEntity(ReservationsEntity entity);
}
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface UsersEntityMapper {
@Mapping(target = "reservations", ignore = true)
User mapFromEntity(FlightAppUsersEntity entity);
@Mapping(target = "reservations", ignore = true)
FlightAppUsersEntity mapToEntity(User user);
}
@Mapper(componentModel = "spring")
public interface ReservationsMapper {
@Mapping(target = "user.userId", source = "user.userId")
ReservationsDTO map(final Reservations reservations);
@Mapping(target = "user.createdAt", ignore = true)
@Mapping(target = "user.active", ignore = true)
@Mapping(target = "user.roles", ignore = true)
@Mapping(target = "createdAt", ignore = true)
@Mapping(target = "user.userId", source = "user.userId")
Reservations map(ReservationsDTO reservationsDTO);
}
@Mapper(componentModel = "spring", uses = ReservationsMapper.class)
public interface UsersMapper {
@Mapping(target = "userId", source = "userId")
UserDTO map(final User user);
@Mapping(target = "roles", ignore = true)
@Mapping(target = "active", ignore = true)
@Mapping(target = "createdAt", ignore = true)
User map(UserDTO userDTO);
}
Код: Выделить всё
@Value
@With
@Builder
@EqualsAndHashCode(of = "reservationId")
@ToString(of = {"reservationId", "origin", "destination", "departureDate", "returnDate", "airline", "price", "currency", "numberOfPassengers", "createdAt", "user"})
public class Reservations {
Integer reservationId;
//some fields
ReservationStatus status;
User user;
}
@Value
@With
@Builder
@EqualsAndHashCode(of = "email")
@ToString(of = {"userId", "firstName", "lastName", "email", "roles", "createdAt", "reservations"})
public class User {
Integer userId;
//some fields
Set roles;
Set reservations;
}
@Getter
@Setter
@EqualsAndHashCode(of = "userId")
@ToString(of = {"userId", "firstName", "lastName", "email", "createdAt"})
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "flightapp_users")
public class FlightAppUsersEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", nullable = false)
private Integer userId;
//some fields
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set reservations;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(
name = "flightapp_user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private Set roles;
}
@Getter
@Setter
@EqualsAndHashCode(of = "reservationId")
@ToString(of = {"reservationId", "user", "origin", "destination", "departureDate", "returnDate", "airline", "price", "currency", "numberOfPassengers", "createdAt"})
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "reservations")
public class ReservationsEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "reservation_id", nullable = false)
private Integer reservationId;
//some fields
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false)
private FlightAppUsersEntity user;
}
< /code>
Example of reservation:
reservation: Reservations(reservationId=29, origin=EWR, destination=LIS, departureDate=2025-02-27T17:40, returnDate=2025-03-01T12:50, airline=TP, price=805.37, currency=EUR, numberOfPassengers=1, createdAt=2025-02-27T04:28:35.877725, user=null)
Подробнее здесь: https://stackoverflow.com/questions/794 ... ct-problem