2012-11-06 95 views
1

我使用休眠和週期在休眠OneToMany關係

有兩個實體(城市和地區)與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. 

是否有任何解決方案這個問題?

回答

0

您需要在轉換爲JSON之前打破實體之間的雙向關係。

我覺得有兩種選擇:

  1. 迭代子集,例如citys in Region並將Region設置爲null。這樣,循環依賴將被打破。您希望在City中添加一個名稱映射屬性regionId,以便關係信息仍可用。

  2. 創建另一組沒有循環依賴的POJO對象,複製實體對象中的值,然後使用POJO對象獲取JSON。