gpt4 book ai didi

java - hibernate 错误: Illegal Attempt to deference collection

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:03 25 4
gpt4 key购买 nike

您好,我的预订和房间之间存在一对多的关系,并且是单向的。预订可能有一到多个房间。现在我正在尝试根据特定日期和房间类型(即特大号床或大号床)搜索是否有可用房间。我的解决方案: 根据并根据日期标准查找预订表中不存在的房间。

房间模型:

@Entity
@Table(name="room")
public class Room implements java.io.Serializable {

private static final long serialVersionUID = 10L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="roomId", nullable = false)
private long Id;

@Column(name="roomNumber", length = 4, nullable = false) //room number with max length of 4 digits
private String roomNumber;

@Column(name="type", nullable = false, length=10) //queen or king
private String roomType;

@Column(name="properties", nullable = false, length=15) //smoking or non-smoking
private String roomProperties;

@Column(name="price", columnDefinition = "DECIMAL(10,2)", nullable = false) //sets the precision of price to 2 decimal places
private double price;

public Room() {}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public long getId() {
return Id;
}

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

public String getRoomNumber() {
return roomNumber;
}

public void setRoomNumber(String roomNumber) {
this.roomNumber = roomNumber;
}

public String getRoomType() {
return roomType;
}

public void setRoomType(String roomType) {
this.roomType = roomType;
}

public String getRoomProperties() {
return roomProperties;
}

public void setRoomProperties(String roomProperties) {
this.roomProperties = roomProperties;
}
}

预订表:

@Entity
@Table(name="Reservation")
public class Reservation implements Serializable {

private static final Long serialVersionUID = 100L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="reservation_Id", nullable = false)
private long Id;

public long getId() {
return Id;
}

public void setId(long id) {
Id = id;
}

@Column(name="CheckInDate")
private Date checkInDate;

@Column(name="CheckOutDate")
private Date checkOutDate;

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "guestId", nullable = false)
private Guest guest;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "ReservedRooms", joinColumns = {@JoinColumn(name="resId",
referencedColumnName = "reservation_Id")}, inverseJoinColumns = {@JoinColumn(name="roomId",
referencedColumnName = "roomId")})
private List<Room> roomList;

@Column(name="roomsWanted")
private int roomsWanted;

public int getRoomsWanted() {
return roomsWanted;
}

public void setRoomsWanted(int roomsWanted) {
this.roomsWanted = roomsWanted;
}

public Date getCheckInDate() {
return checkInDate;
}

public void setCheckInDate(Date checkInDate) {
this.checkInDate = checkInDate;
}

public Date getCheckOutDate() {
return checkOutDate;
}

public void setCheckOutDate(Date checkOutDate) {
this.checkOutDate = checkOutDate;
}

public Guest getGuest() {
return guest;
}

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

public List<Room> getRoomList() {
return roomList;
}

public void setRoomList(List<Room> roomList) {
this.roomList = roomList;
}
}

现在执行搜索可用性的方法:

@Override
@Transactional
@SuppressWarnings("unchecked")
public boolean checkAvailability(SearchCriteria searchCriteria) {
String hql = "from Room as r where r.roomType = :roomType1 and r.roomProperties = :roomProperties1 " +
"and r.Id not in (Select res.roomList.Id from Reservation as res left outer join res.roomList " +
"where res.checkInDate <=:checkInDate1 and res.checkOutDate >= :checkOutDate1 " +
" and R.Id = res.roomList.Id) ";
Query query = getSession().createQuery(hql);
query.setParameter("roomType1", searchCriteria.getRoomType());
query.setParameter("roomProperties1", searchCriteria.getRoomProperties());
query.setParameter("checkInDate1", searchCriteria.getCheckInDate());
query.setParameter("checkOutDate1", searchCriteria.getCheckOutDate());

List<Room> roomList = query.list();
if(roomList.isEmpty()) {
return true;
}
return false;
}

但它提示并给出错误:

illegal attempt to dereference collection [reservatio1_.reservation_Id.roomList] with element property reference [Id]

请告诉我我做错了什么,因为我是 hibernate 新手

最佳答案

当您加入集合时,您必须为其命名。您不能直接使用它(取消引用)。

in (Select ROOMS.Id from Reservation as res 
left outer join res.roomList AS ROOMS
where res.checkInDate <=:checkInDate1 and res.checkOutDate >= :checkOutDate1
and R.Id = ROOMS.Id)

关于java - hibernate 错误: Illegal Attempt to deference collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34425742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com