【问题标题】:How to get specified fields from the Object, to check if List contains() Object? Room booking validaton problem如何从对象中获取指定字段,以检查列表是否包含()对象?房间预订验证问题
【发布时间】:2019-05-16 19:44:06
【问题描述】:

这是我的实体:

 @Entity
@Table(name = "reservation")
public class Reservation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "date_in")
    private LocalDate dateIn;

    @Column(name = "date_out")
    private LocalDate dateOut;

    @ManyToOne
    private Guest guest;

    @ManyToOne
    private Room room;

    public Reservation() {
    }

    public Reservation(LocalDate dateIn, LocalDate dateOut, Guest guest, Room room) {
        this.dateIn = dateIn;
        this.dateOut = dateOut;
        this.guest = guest;
        this.room = room;
    }

    public Guest getGuest() {
        return guest;
    }

    public void setGuest(Guest guest) {
        this.guest = guest;
    }

    public Room getRoom() {
        return room;
    }

    public void setRoom(Room room) {
        this.room = room;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDate getDateIn() {
        return dateIn;
    }

    public void setDateIn(LocalDate dateIn) {
        this.dateIn = dateIn;
    }

    public LocalDate getDateOut() {
        return dateOut;
    }

    public void setDateOut(LocalDate dateOut) {
        this.dateOut = dateOut;
    }

    @Override
    public String toString() {
        return "Reservation{" +
                "id=" + id +
                ", dateIn=" + dateIn +
                ", dateOut=" + dateOut +
                ", guest=" + guest +
                ", room=" + room +
                '}';
    }
}

这是我的验证方法:

@覆盖 公共预订保存(预订预订){ validateDate(reservation.getDateIn(), reservation.getDateOut());

        List<ReservationRepository> bookedRooms = reservationRepository.findBookedRooms();

        if (!bookedRooms.contains(reservation)) {
            return reservationRepository.save(reservation);
        }
        return null;
    }

findBookedRooms() 得到这个 sql 查询:

@Query(value = "SELECT rs.room_id, rs.date_in, rs.date_out FROM reservation rs LEFT JOIN room r ON rs.room_id = r.id WHERE rs.id IS NOT NULL", nativeQuery = true)
    List<ReservationRepository> findBookedRooms();

问题: 在 if 语句中,bookedRooms 获取 List of:reservation.room_id、reservation.date_in、reservation.date_out 参数,而 reservation Object 获取:reservation.id、reservation.date_in、reservation.date_out。 因此我们有 rs.room_id - rs.id 冲突,所以“if 语句”将始终为真。我怎样才能让我的预订对象获得这些特定参数:reservation.getRoom().getNumber()、预订。日期输入, reservation.date_out?

我需要此验证来检查在预订特定房间时是否存在日期冲突。

【问题讨论】:

    标签: java hibernate model-view-controller


    【解决方案1】:

    您的验证包括使用从数据库检索到的列表中的contains 方法,检查:“列表包含此预订?”。

    您的问题基本上是说 IF 语句始终评估为真,因此它始终评估为:“此保留不在列表中!”

    考虑到这一切,我相信你应该检查一下List接口中的contains方法是如何工作的。这个post对此有一些见解。

    contains 方法使用equals 方法来检查一个对象是否在列表中具有相等的对象。由于使用的两种类型不同(ReservationRepositoryReservation 不同)它将在 100% 的情况下失败,除非您开始覆盖 equals 方法(考虑到您使用两种类型进行比较,我会警告您,不要那样做)。

    可能的解决方案:

    循环遍历列表并在循环内检查评估旅游数据库中是否已存在预订所需的字段。在您的保存方法中执行此操作,或编写其他方法以更好地组织代码。

    循环可能如下所示:

    boolean reservationExists = false;
    for(ReservationRepository r:bookedRooms){
         if (r.getRoom().equals(reservation.getRoom()) &&
            r.getDateOut().equals(reservation.getDateOut()) &&
            r.getDateIn().equals(reservation.getDateIn())) {
                reservationExists = true;
                break;
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 2022-11-09
      • 2018-11-09
      • 2012-05-24
      • 1970-01-01
      相关资源
      最近更新 更多