我遇到了命名查询的问题。首先,我想了解一个可能的原因可能是一个原因(一些beyound语法错误)。其次,我很高兴听到你关于可能解决方案的问题。提前谢谢。
我有一个NamedQuery:
@NamedQuery(name = "HotelsEntity.findAllWithFilter",
query = "select h from HotelsEntity h " +
/*"left join fetch h.reservationDates hReservationDates " +*/
"left join fetch h.property hProperty " +
"left join fetch h.rooms hRooms " +
"left join fetch hRooms.inventory inventory " +
"left join fetch inventory.reservation reservation " +
"where " +
"hProperty.hasPool = :pool " +
"AND hProperty.hasTennisCourt = :tennis " +
"AND hProperty.hasWaterslides = :waterslides " +
"AND hRooms.peopleCapacity >= :capacity " +
"AND hRooms.locked = false " +
"AND (" +
"(reservation.checkIn <= :checkin AND reservation.checkOut >= :checkin) " +
"OR " +
"(reservation.checkIn >= :checkin AND reservation.checkOut <= :checkout) " +
"OR " +
"(reservation.checkIn >= :checkin AND reservation.checkOut >= :checkout) )"),
它有效。我想再向ReservationDatesEntity添加一个连接:
@NamedQuery(name = "HotelsEntity.findAllWithFilter",
query = "select h from HotelsEntity h " +
"left join fetch h.reservationDates hReservationDates " +
"left join fetch h.property hProperty " +
"left join fetch h.rooms hRooms " +
"left join fetch hRooms.inventory inventory " +
"left join fetch inventory.reservation reservation " +
"where " +
"hProperty.hasPool = :pool " +
"AND hProperty.hasTennisCourt = :tennis " +
"AND hProperty.hasWaterslides = :waterslides " +
"AND hRooms.peopleCapacity >= :capacity " +
"AND hRooms.locked = false " +
"AND (" +
"(reservation.checkIn <= :checkin AND reservation.checkOut >= :checkin) " +
"OR " +
"(reservation.checkIn >= :checkin AND reservation.checkOut <= :checkout) " +
"OR " +
"(reservation.checkIn >= :checkin AND reservation.checkOut >= :checkout) )"),
它与org.hibernate.HibernateException: Errors in named queries: HotelsEntity.findAllWithFilter
失败了。命名查询中错误的原因是什么?
public class HotelsEntity implements HospitalityEntity{
private int idHotel;
private String name;
private String region;
private String description;
private String photo;
private HotelPropertyEntity property;
private List<RoomEntity> rooms;
private List<ReservationDatesEntity> reservationDates;
@OneToMany(mappedBy = "hotel")
public List<ReservationDatesEntity> getReservationDates() {
return reservationDates;
}
public void setReservationDates(List<ReservationDatesEntity> reservationDates) {
this.reservationDates = reservationDates;
}
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_hotel_property")
public HotelPropertyEntity getProperty() {
return property;
}
public void setProperty(HotelPropertyEntity property) {
this.property = property;
}
@OneToMany(mappedBy = "hotel")
public List<RoomEntity> getRooms() {
return rooms;
}
public void setRooms(List<RoomEntity> rooms) {
this.rooms = rooms;
}
@Id
@Column(name = "id_hotel", unique = true)
@GeneratedValue(strategy=GenerationType.AUTO)
public int getIdHotel() {
return idHotel;
}
public void setIdHotel(int idHotel) {
this.idHotel = idHotel;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "region")
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
@Basic
@Column(name = "description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Basic
@Column(name = "photo")
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
和ReservationDatesEntity:
@Entity
@Table(name = "reservation_dates", schema = "", catalog = "mydb",
uniqueConstraints = {
@UniqueConstraint(columnNames = "id")
})
public class ReservationDatesEntity implements HospitalityEntity {
private int id;
private long checkIn;
private long checkOut;
private int hotelId;
private HotelsEntity hotel;
@JsonBackReference
@ManyToOne
@JoinColumn (name = "id_hotel", insertable = false, updatable = false )
public HotelsEntity getHotel() {
return hotel;
}
public void setHotel(HotelsEntity hotel) {
this.hotel = hotel;
}
@Id
@Column(name = "id", columnDefinition="bigint(20)")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "id_hotel", columnDefinition="bigint(20)")
public int getHotelId() {
return hotelId;
}
public void setHotelId(int hotelId) {
this.hotelId = hotelId;
}
@Basic
@Column(name = "check_in", columnDefinition="bigint(20)")
public Long getCheckIn() {
return checkIn;
}
public void setCheckIn(Long checkIn) {
this.checkIn = checkIn;
}
@Basic
@Column(name = "checkout", columnDefinition="bigint(20)")
public Long getCheckOut() {
return checkOut;
}
public void setCheckOut(Long checkOut) {
this.checkOut = checkOut;
}
}