简体   繁体   English

与HQL结合使用

[英]Using not in with join and between with HQL

I'm trying to convert this postgresql to hql, but I can't figure it out: I don't know if I can use between here or just in criteria. 我正在尝试将此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
    )

This query will return a List, I need to map the return to this list or create a native query to do it 该查询将返回一个列表,我需要将返回值映射到此列表或创建本机查询来执行此操作

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;
    }



}

Thanks for helping. 感谢您的帮助。

You can use SqlQuery in Hibernate instead of translating it to HQL. 您可以在Hibernate中使用SqlQuery而不是将其转换为HQL。 I mean you can use the same SQL you have above and make Hibernate to return Room entities that matches that result. 我的意思是,您可以使用与上面相同的SQL,并使Hibernate返回与该结果匹配的Room实体。 Please, take a look at https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html for more information. 请查看https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM