@OneToMany和@ManyToOne @Formula之间的双向关系返回null

时间:2018-12-18 03:13:09

标签: java mysql spring hibernate jpa

基本上,我有两个实体。

  • 预订
  • 房间

Reservation实体与Room实体具有一对多关系。同样,Room实体与Reservation实体具有多对一关系。从ReservationRoom的关系一直存在。但是从RoomReservation的关系仅在保留状态标记为“已签入”时才存在(为此,我使用枚举序数。ReservationStatus.CHECKED_IN的序数为{{1 }}。

1注释足以显示一对多关系。对于多对一关系,我使用@OneToMany@ManyToOne批注。我是Hibernate的新手。因此,我在MySQL控制台上对公式进行了测试。

这是我运行的MySQL查询,我得到了结果。     选择*从预订r左加入r.identifier = s.reservation_identifier进入s.rooms_identifier = m.identifier的加入房间m,其中r.reservation_status = 1和m.identifier = 4

@Formula类中的m.identifier = 4m.identifier = identifier替换。上面的查询在MySQL控制台中运行良好。但是,当我在应用程序中运行它时,它总是返回Room

这是null类的源。

Room

这是@Entity public class Room { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer identifier; private Integer propertyIdentifier; private String number; @ManyToOne(cascade = CascadeType.ALL) private RoomType roomType; @ManyToOne(cascade = CascadeType.ALL) private Floor floor; @ManyToOne(cascade = CascadeType.ALL) private Block block; // select * from reservation r left join reservation_rooms s on r.identifier = s.reservation_identifier left join room m on s.rooms_identifier = m.identifier where r.reservation_status = 1 and m.identifier = 4 @Formula(value = "select r from reservation r left join reservation_rooms s on r.identifier = s.reservation_identifier left join room m on s.rooms_identifier = m.identifier where m.identifier = identifier and r.reservation_status = 1") @ManyToOne(cascade = CascadeType.ALL) private Reservation occupantReservation; private Integer statusIdentifier; public Integer getIdentifier() { return identifier; } public void setIdentifier(Integer identifier) { this.identifier = identifier; } public Integer getPropertyIdentifier() { return propertyIdentifier; } public void setPropertyIdentifier(Integer propertyIdentifier) { this.propertyIdentifier = propertyIdentifier; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Integer getStatusIdentifier() { return statusIdentifier; } public void setStatusIdentifier(Integer statusIdentifier) { this.statusIdentifier = statusIdentifier; } public RoomType getRoomType() { return roomType; } public void setRoomType(RoomType roomType) { this.roomType = roomType; } public Floor getFloor() { return floor; } public void setFloor(Floor floor) { this.floor = floor; } public Block getBlock() { return block; } public void setBlock(Block block) { this.block = block; } @Override public boolean equals(Object object) { boolean result = false; if (object == this) { result = true; } else if ((object instanceof Room)) { Room other = (Room) object; if (other.identifier.equals(identifier)) { result = true; } } return result; } @Override public int hashCode() { return identifier; } public Reservation getOccupantReservation() { return occupantReservation; } public void setOccupantReservation(Reservation occupantReservation) { this.occupantReservation = occupantReservation; } } 类的源。

Reservation

这是来自MySQL控制台的日志。

@Entity
public class Reservation {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer identifier;

    @ManyToOne(cascade = CascadeType.ALL)
    private Property property;

    @ManyToOne(cascade = CascadeType.ALL)
    private Guest guest;

    @Temporal(TemporalType.DATE)
    private Date arrival;

    @Temporal(TemporalType.DATE)
    private Date departure;

    private Integer nights;

    private Integer adults;

    private Integer children;

    @OneToMany(cascade = CascadeType.ALL)
    private Set<Room> rooms;

    @Enumerated(value = EnumType.ORDINAL)
    private ReservationSource source;

    private ReservationStatus reservationStatus;

    public Integer getIdentifier() {
        return identifier;
    }

    public void setIdentifier(Integer identifier) {
        this.identifier = identifier;
    }

    public Guest getGuest() {
        return guest;
    }

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

    public Date getArrival() {
        return arrival;
    }

    public void setArrival(Date arrival) {
        this.arrival = arrival;
    }

    public Date getDeparture() {
        return departure;
    }

    public void setDeparture(Date departure) {
        this.departure = departure;
    }

    public Integer getNights() {
        return nights;
    }

    public void setNights(Integer nights) {
        this.nights = nights;
    }

    public Integer getAdults() {
        return adults;
    }

    public void setAdults(Integer adults) {
        this.adults = adults;
    }

    public Integer getChildren() {
        return children;
    }

    public void setChildren(Integer children) {
        this.children = children;
    }

    public Property getProperty() {
        return property;
    }

    public void setProperty(Property property) {
        this.property = property;
    }

    public ReservationStatus getReservationStatus() {
        return reservationStatus;
    }

    public void setReservationStatus(ReservationStatus reservationStatus) {
        this.reservationStatus = reservationStatus;
    }

    public void setRooms(Set<Room> rooms) {
        this.rooms = rooms;
    }

    public Set<Room> getRooms() {
        return rooms;
    }

    public ReservationSource getSource() {
        return source;
    }

    public void setSource(ReservationSource source) {
        this.source = source;
    }
}

您能指导我做错什么吗?谢谢您的宝贵时间。

1 个答案:

答案 0 :(得分:0)

我认为使用过滤器会更适合您的情况:

1)Reservation上定义过滤器:

@Entity
@FilterDef(name="reservationFilter", defaultCondition="reservation_status = 1")
@Entity
public class Reservation {

2),然后在@ManyToOne

@ManyToOne(cascade = CascadeType.ALL)
@Filter(name = "reservationFilter")
private Reservation occupantReservation;

3)在查询前启用:

Session session = sessionFactory.getCurrentSession();
session.enableFilter("reservationFilter");