Java 用于hibernate映射的JsonIdentityInfo

Java 用于hibernate映射的JsonIdentityInfo,java,json,hibernate,rest,jsonidentityinfo,Java,Json,Hibernate,Rest,Jsonidentityinfo,我有两个实体,城市和类型,以及这两者之间的多对多关系。我需要的是: 包含类型的城市json 包含城市的json类型 我正在使用JsonIdentityInfo来阻止映射的无限递归,但我从JSON得到的东西并没有真正帮助我。 这是我目前从城市JSON得到的 0: { @idType: 1 id: 1 name: "destination" cities: [2] - 0: { @idCity: 2

我有两个实体,城市和类型,以及这两者之间的多对多关系。我需要的是:

  • 包含类型的城市json
  • 包含城市的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
    
    城市也是如此。我需要它是双向的

    这是我的实体的代码:

    城市:

    @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
    }
    
    @实体
    @表(name=“City”)
    @XmlRootElement
    @JsonIdentityInfo(generator=ObjectiveGenerators.IntSequenceGenerator.class,属性=“@idCity”)
    公营城市{
    @身份证
    @GeneratedValue(策略=GenerationType.AUTO)
    @列(name=“idCity”)
    私有int-id;
    @列(name=“name”)
    私有字符串名称;
    @列(name=“description”)
    私有字符串描述;
    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name=“CityType”,joinColumns={@JoinColumn(name=“idCity”)},inverseJoinColumns={
    @JoinColumn(name=“idType”)})
    私有集类型=新HashSet();
    …接球手和接球手
    }
    
    类型:

    @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
    }
    
    @实体
    @表(name=“Type”)
    @XmlRootElement
    @JsonIdentityInfo(generator=ObjectiveGenerators.IntSequenceGenerator.class,属性=“@idType”)
    公共类类型{
    @身份证
    @GeneratedValue(策略=GenerationType.AUTO)
    @列(name=“idType”)
    私有int-id;
    @列(name=“name”)
    私有字符串名称;
    @ManyToMany(fetch=FetchType.EAGER,mappedBy=“types”)
    私有集cities=新HashSet();
    …接球手和接球手
    }
    
    更改线路

    @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@idType")
    to
    @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id")
    

    在这两个类上。

    如果我这样做,唯一的区别是我有“id:3”,但没有对象的json。有没有一种方法可以解析json,即使它只引用了对象的id?我不明白你想要什么,你能详细解释一下吗。