1

City と Type という 2 つのエンティティがあり、これら 2 つの間に多対多の関係があります。私が必要とするのは:

  1. タイプを含む City の json
  2. 都市を含む Type の 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...."
           }
}

Cities 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
}
4

1 に答える 1