如何在hibernate中防止无限循环

时间:2014-05-05 15:15:05

标签: java hibernate

我有一个使用此API的休息服务器和客户端。我有一个酒店列表,它已经通过,直到我向其他实体添加双向依赖。之后我开始收到无数的实体,只是在数据库中重复相同的行。

这是我的第一个hibernate项目,所以我可能会对新手犯下琐碎的错误。

Schema

地图:

@Entity
@Table(name = "hotels", schema = "", catalog = "mydb")
public class HotelsEntity implements HospitalityEntity{
private int idHotel;
private String name;
private String region;
private String description;

// private byte [] photo;

private HotelPropertyEntity property;
private List<RoomEntity> rooms;


@OneToOne(mappedBy = "hotel")
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;
}

酒店物业

@Entity
@Table(name = "hotel_property", schema = "", catalog = "mydb")
public class HotelPropertyEntity {
private int idHotelProperty;
private byte hasPool;
private byte hasTennisCourt;
private byte hasWaterslides;

private HotelsEntity hotel;


@Id
@Column(name = "id_hotel_property", unique = true)
@GeneratedValue(strategy=GenerationType.AUTO)
public int getIdHotelProperty() {
    return idHotelProperty;
}

public void setIdHotelProperty(int idHotelProperty) {
    this.idHotelProperty = idHotelProperty;
}

@Basic
@Column(name = "has_pool", columnDefinition = "BIT", length = 1)
public byte getHasPool() {
    return hasPool;
}

public void setHasPool(byte hasPool) {
    this.hasPool = hasPool;
}

@Basic
@Column(name = "has_tennis_court", columnDefinition = "BIT", length = 1)
public byte getHasTennisCourt() {
    return hasTennisCourt;
}

public void setHasTennisCourt(byte hasTennisCourt) {
    this.hasTennisCourt = hasTennisCourt;
}

@Basic
@Column(name = "has_waterslides", columnDefinition = "BIT", length = 1)
public byte getHasWaterslides() {
    return hasWaterslides;
}

public void setHasWaterslides(byte hasWaterslides) {
    this.hasWaterslides = hasWaterslides;
}

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id_hotel_property")
public HotelsEntity getHotel() {
    return hotel;
}

public void setHotel(HotelsEntity hotel) {
    this.hotel = hotel;
}

间:

@Entity
@Table(name = "room", schema = "", catalog = "mydb")
public class RoomEntity {
private int idRoom;
private String roomType;
private int peopleCapacity;
private Boolean  booked;
private Boolean locked;

private HotelsEntity hotel;
private InventoriesEntity inventory;
private RoomPropertyEntity roomProperty;

@OneToOne(mappedBy = "room")
public RoomPropertyEntity getRoom() {
    return roomProperty;
}

public void setRoom(RoomPropertyEntity roomProperty) {
    this.roomProperty = roomProperty;
}

@OneToOne
@JoinColumn(name = "id_room")
public InventoriesEntity getInventory() {
    return inventory;
}

public void setInventory(InventoriesEntity inventory) {
    this.inventory = inventory;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_hotel")
public HotelsEntity getHotel() {
    return hotel;
}

public void setHotel(HotelsEntity hotel) {
    this.hotel = hotel;
}

@Id
@Column(name = "id_room")
public int getIdRoom() {
    return idRoom;
}

public void setIdRoom(int idRoom) {
    this.idRoom = idRoom;
}

@Basic
@Column(name = "room_type")
public String getRoomType() {
    return roomType;
}

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

@Basic
@Column(name = "people_capacity")
public int getPeopleCapacity() {
    return peopleCapacity;
}

public void setPeopleCapacity(int peopleCapacity) {
    this.peopleCapacity = peopleCapacity;
}

@Basic
@Column(name = "booked", columnDefinition = "BIT", length = 1)
public Boolean getBooked() {
    return booked;
}

public void setBooked(Boolean  booked) {
    this.booked = booked;
}

@Basic
@Column(name = "locked", columnDefinition = "BIT", length = 1)
public Boolean  getLocked() {
    return locked;
}

public void setLocked(Boolean  locked) {
    this.locked = locked;
}

请问你能告诉hibernate停止这个循环的方法是什么?

P.S 此代码包含另一个问题。如果我删除了一对一的依赖关系,并且只收到一对多的我收到failed to lazily initialize a collection of role: com.example.model.HotelsEntity.rooms, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.example.model.HotelsEntity["rooms"])

1 个答案:

答案 0 :(得分:1)

您需要将实体标记为不可序列化为JSON。请在关系的一侧使用@JsonIgnore或@JsonIgnoreProperties(“field”)(注释是类级别)。