Hibernate错误:非法尝试deference收集

时间:2015-12-22 22:49:16

标签: java hibernate spring-boot

您好我在预订和房间之间有一对多的关系,它是单向的。预订可能有一到几个房间。现在我正在尝试根据特定日期和房间类型(即国王或王后)搜索房间是否可用。 我的解决方案    查找基于预订表的房间以及基于日期标准的房间。

房间型号:

@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]

请知道我做错了,因为我刚接触休眠

1 个答案:

答案 0 :(得分:0)

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

GridView