我有日历和房间实体。
我的日历实体如下所示:
public class CalendarEntity {
@EmbeddedId
private CalendarId calendarId = new CalendarId();
@Basic
@Column(name = "available", nullable = false)
private Boolean available;
@Basic
@Column(name = "price")
private double price;
@ManyToOne
@JoinColumn(name = "room_id", referencedColumnName = "id", insertable =
false, updatable = false)
private RoomEntity roomEntity;
}
@Embeddable
public class CalendarId implements Serializable {
@Column(name= "date", nullable = false)
private Date date;
@Column(name = "room_id", nullable = false)
private Integer room_id;
}我的房间实体看起来是这样的:
public class RoomEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id", nullable = false, unique = true)
private Integer id;
}假设我在日历实体中有一些关于日期为2017-10-5,2017-10-6,2017-10-7,2017-10-8和room_id=10的条目。
我想要构造一个查询,以便如果用户要求一个房间有一个特定的签出日期和结账日期,那么他将得到正确的房间列表。
我怎么能这么做?
例如,用户要求房间签入=2017-10-6和结账=2017-10-8,房间应该出现在列表中。
查询应该是什么,这样我才能得到正确的房间列表?
更新:
目前,我的查询只检查房间在签出日期和退房日期是否可用,但不在两者之间:
select r from RoomEntity r where r.id in (Select c1.calendarId.room_id from CalendarEntity c1, CalendarEntity c2 where (c1.calendarId.room_id = c2.calendarId.room_id) and (c1.calendarId.date =:checkin and c1.available=true) and(c2.calendarId.date =:checkout and c2.available=true))发布于 2017-07-11 18:51:40
一种方法是:选择count( c.calendarId.room_id ),选择CalendarEntity c中的c.calendarId.room_id,其中c.available = true,c.calendarId.date介于:checkin和: checkin之间,结帐组由c.calendarId.room_id拥有计数(c.calendarId.room_id) >= :noOfDays noODays表示从签出中计算出来的天数,以及需要预订的签出日期。
发布于 2017-07-11 19:05:07
SELECT r FROM RoomEntity r
WHERE r.id
in (select c1.calendarId.room_id
FROM CalendarEntity c1 JOIN CalendarEntity c2
WHERE c1.calendarId.room_id = c2.calendarId.room_id
AND ((c1.calendarId.date BETWEEN :checkin AND :checkout)
AND (c1.available=true and c2.available=true));https://stackoverflow.com/questions/45041341
复制相似问题