I have two entities City and Type and a many-to-many relationship between these two. What I need is:
- a json for City which contains the types
- a json for Type which contains the cities.
I am using JsonIdentityInfo to stop the infinite recursion from mapping, but what I get from JSON doesn't really help me. This is what I currently get from the cities 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
But what I need is something like this:
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...."
}
}
And the same thing for Cities JSON. I need it to be bidirectional.
This is the code for my entities:
City:
@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
}
Type:
@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
}
Chnage the line
on both the classes.