1

我使用休眠和

有两个具有 OneToMany 关系的实体(City 和 Region)。

首先:

 @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 获取简单的 Object(City) 时,它会返回循环:

     {"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.

这个问题有什么解决方案吗?

4

1 回答 1

0

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

我认为有两种选择:

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

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

于 2012-11-06T03:58:15.377 回答