0
我有兩個實體City和Type以及這兩者之間的多對多關係。我需要的是:JsonIdentityInfo用於休眠映射
- 爲市JSON其中包含了類型
- A型JSON其中包含了城市。
我正在使用JsonIdentityInfo來停止映射的無限遞歸,但是我從JSON獲得的內容並不能真正幫助我。 這是我目前從城市得到JSON
0: {
@idType: 1
id: 1
name: "destination"
cities: [2]
- 0: {
@idCity: 2
id: 3
name: "Zalau"
description: "City...."
types: [2] <---- I don't need this because I'm already in types JSON
- 0: {
@idType: 3
id: 2
name: "other type"
cities: [1]
- 0: 2
}
- 1: 1 <----- end of don't need
}
- 1: {
@idCity: 4
id: 0
name: "Cluj"
description: "City2..."
types: [1] <---- don't need
- 0: 1
}
}
1: 3 <----- I want to be the Type with id 3 although it was already generated from a city
但我需要的是這樣的:
0: {
@idType: 1
id: 1
name: "destination"
cities: [2]
- 0: {
@idCity: 2
id: 3
name: "Zalau"
description: "City...."
}
- 1: {
@idCity: 4
id: 0
name: "Cluj"
description: "City2..."
}
}
1: {
@idType: 3
id: 2
name: "other type"
cities: [1]
- 0: {
@idCity: 2
id: 3
name: "Zalau"
description: "City...."
}
}
,同樣的事情對城市JSON。我需要它是雙向的。
這是我的實體代碼:
市:
@Entity
@Table(name = "City")
@XmlRootElement
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@idCity")
public class City {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idCity")
private int id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "CityType", joinColumns = { @JoinColumn(name = "idCity") }, inverseJoinColumns = {
@JoinColumn(name = "idType") })
private Set<Type> types = new HashSet<Type>();
.... getters and setters
}
類型:
@Entity
@Table(name = "Type")
@XmlRootElement
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@idType")
public class Type {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idType")
private int id;
@Column(name = "name")
private String name;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "types")
private Set<City> cities = new HashSet<City>();
.... getters and setters
}
如果我這樣做,唯一的區別是我有「id:3」但不是對象的json。有沒有辦法解析json,即使它只有對象的id的引用? –
我不明白你想要什麼,請你詳細說明。 –