gpt4 book ai didi

java - Hibernate OneToMany 关系中的循环

转载 作者:行者123 更新时间:2023-12-01 05:02:09 27 4
gpt4 key购买 nike

我使用 Hibernate 和

有两个具有 OneToMany 关系的实体(城市和地区)。

第一个:

 @Entity
@Table(name = "p_region")
public class Region implements Serializable{

@OneToMany(mappedBy = "region",fetch= FetchType.LAZY, cascade = CascadeType.MERGE)
private List<City> citys;

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;


private String name;

//++++++++++++++++++++ GETSET

public List<City> getCitys() {
return citys;
}

public void setCitys(List<City> citys) {
this.citys = citys;
}

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

第二个:

    @Entity
@Table(name = "p_city")
public class City implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@NotEmpty(message = "Название не должно быть пустым")
@Length(max = 10, min = 2, message = "Название должно быть менее 2 символов и не
более 100")
private String cityName;

@NotEmpty(message = "Код города не должно быть пустым")
private String cityCode;
@Column(name = "zone")
private Integer zone;


@Basic(optional = true)
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date entryDate = Calendar.getInstance().getTime();

@ManyToOne()
private Region region;

@Basic(optional = true)
private String zip_code;

// GET SET ::::::::::::::::::::::::::::::::::
public Integer getZone() {
return zone;
}

public void setZone(Integer zone) {
this.zone = zone;
}
public Region getRegion() {
return region;
}

public void setRegion(Region region) {
this.region = region;
}

public void delete() {
System.out.println("QQQQQQQQQQQQQQQQQQQQQQ");
}

public String getCityCode() {
return cityCode;
}

public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}

public String getCityName() {
return cityName;
}

public void setCityName(String cityName) {
this.cityName = cityName;
}

public Date getEntryDate() {
return entryDate;
}

public void setEntryDate(Date entryDate) {
this.entryDate = entryDate;
}

public Integer getId() {
return id;
}

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

public String getZip_code() {
return zip_code;
}

public void setZip_code(String zip_code) {
this.zip_code = zip_code;
}

}

当我尝试使用 JSON 获取简单对象(城市)时,它会返回周期:

     {"id":577,"region":{"name":"нет региона","id":15,"citys":[{"id":577,"region":  
{"name":"нет региона","id":15,"citys":[{"id":577,"region":{"name":"нет
региона","id":15,"citys":[{"id":577,"region":{"name":"нет
региона","id":15,"citys":[{"id":577,"region":{"name":"нет
региона","id":15,"citys":[{"id":577,"region":{"name":"нет
региона","id":15,"citys":[{"id":577,"region":{"name":"нет......so on.

这个问题有解决办法吗?

最佳答案

在转换为 JSON 之前,您需要打破实体之间的双向关系。

我认为有两种选择:

  1. 迭代子集合,例如Region 中的 city 并将 Region 设置为 null。这样,循环依赖就会被打破。您可能想在 City 中添加一个名称映射属性 regionId,以便关系信息仍然可用。

  2. 创建另一组没有循环依赖的 POJO 对象,从实体对象复制值,然后使用 POJO 对象获取 JSON。

关于java - Hibernate OneToMany 关系中的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13244039/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com