不使用with和HQL之间

时间:2015-12-29 13:34:41

标签: java hibernate postgresql hql jpql

我正在尝试将此postgresql转换为hql,但我无法弄清楚: 我不知道我是否可以在这里使用或只是在标准中使用。

SELECT * FROM room where id not in(
    SELECT room.id FROM room  LEFT
    OUTER  JOIN reservation_room ON
    room.id = reservation_room.roomid
    left outer join reservation on
    reservation_room.reservationid = reservation.id
    where :startdate between reservation.checkin_date and reservation.checkout_date - interval '1' day or
    :enddate between reservation.checkin_date + interval '1' day and reservation.checkout_date
    )

此查询将返回一个List,我需要将返回映射到此列表或创建一个本机查询来执行此操作

package com.hotel.model;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * The persistent class for the room database table.
 * 
 */
@Entity
@Table(name = "room")
public class Room implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "ROOM_ID_GENERATOR", sequenceName = "room_id_seq", allocationSize = 1, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ROOM_ID_GENERATOR")
    @Column(unique = true, nullable = false)
    private Long id;

    @Column(name = "room_number")
    private Integer roomNumber;

    @Column(length = 1)
    private String occupied;

    // bi-directional many-to-one association to RoomType
    @ManyToOne
    @JoinColumn(name = "room_typeid", nullable = false)
    private RoomType roomType;

    // bi-directional many-to-many association to Reservation
    @ManyToMany
    @JoinTable(name = "reservation_room", joinColumns = {
            @JoinColumn(name = "reservationid", nullable = false)
    }, inverseJoinColumns = {
            @JoinColumn(name = "roomid", nullable = false)
    })
    private List<Reservation> reservations;

    public Room() {
    }

    public Long getId() {
        return this.id;
    }

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

    public Integer getRoomNumber() {
        return this.roomNumber;
    }

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

    public String getOccupied() {
        return this.occupied;
    }

    public void setOccupied(final String occupied) {
        this.occupied = occupied;
    }

    public RoomType getRoomType() {
        return this.roomType;
    }

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

    public List<Reservation> getReservations() {
        return this.reservations;
    }

    public void setReservations(final List<Reservation> reservations) {
        this.reservations = reservations;
    }



}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以在Hibernate中使用SqlQuery,而不是将其转换为HQL。我的意思是你可以使用你上面的相同的SQL,并让Hibernate返回匹配该结果的Room实体。请查看https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html以获取更多信息。