循环使用Hibernate OneToMany关系

时间:2012-11-06 03:43:20

标签: java json spring hibernate jpa

我使用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获取简单的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.

此问题是否有解决方案?

1 个答案:

答案 0 :(得分:0)

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

我认为有两种选择:

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

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