json 多对多/循环参考问题Jackson+Spring+Hibernate

ulmd4ohb  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(64)

Course.java

@Entity
@Table
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Course {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotEmpty
    private String description;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "subject_course", joinColumns = @JoinColumn(name = "subject_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "course_id", referencedColumnName = "id"))
    private Set<Subject> subjects = new HashSet<Subject>();

    ---- getter/setter ----

字符串

Subject.java

@Entity
@Table
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Subject {

    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String description;

    @ManyToMany(mappedBy = "subjects", fetch=FetchType.EAGER)
    @Cascade({CascadeType.DELETE, CascadeType.SAVE_UPDATE})
    private Set<Course> courses = new HashSet<Course>();

    ---- getter/setter ----


Spring中的请求配置:

@RequestMapping(value = "/courses", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<?> getAllCourses() {
    List<Course> courses = courseService.getAllCourses();
    if (courses.isEmpty()) {
        return new ResponseEntity<Message>(new Message("error", "No course found!"), HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<List<Course>>(courses, HttpStatus.OK);
}

**Hibernate版本:4.2.0.Final

Spring版本:3.2.8.RELEASE
Jackson:**

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.2.4</version>
 </dependency>

预期O/P

[{
      "id": 1,
      "description": "BCA",
      "subjects":[{
           "id":1,
           "description":"Physics",
           "courses":[1,2,3] //Either show blank array or only ids
      }]
 },{
      "id": 2,
      "description": "BSC",
      "subjects":[{
           "id":1,
           "description":"Physics",
           "courses":[1,2,3]
      }]
 },{
      "id": 3,
      "description": "BA",
      "subjects":[{
           "id":1,
           "description":"Physics",
           "courses":[1,2,3]
      }]
 },]


但是得到O/P:

[
   {
     "id": 1,
     "description": "BCA",
     "subjects": [
       {
         "id": 1,
         "description": "Math",
         "staffs": [],
         "courses": [
           {
             "id": 4,
             "description": "BDA",
             "subjects": [
               1
             ],
             "students": []
           },
           {
             "id": 3,
             "description": "BBA",
             "subjects": [
               1
             ],
             "students": []
           },
           1
         ],
         "students": []
       }
     ],
     "students": [
       {
         "id": 1,
         "name": "",
         "age": 0,
         "gender": null,
         "course": 1,
         "subjects": []
       }
     ]
   },
   3,
   4
 ]


根据实际的o/p,它在第二级停止递归。但我的要求是不要从child重复相同的对象数据.这意味着Course不能在Subject的course属性中重复它的数据。同样,如果从Subject调用相同的对象,那么subject不应该重复Course subject属性值。最好跳过,如果不能,那么只显示用逗号分隔的id值。
请建议如何解决此问题。

kpbwa7wx

kpbwa7wx1#

你可以像这样在Subject类中使用@JsonIgnore

@ManyToMany(mappedBy = "subjects", fetch=FetchType.EAGER)
@Cascade({CascadeType.DELETE, CascadeType.SAVE_UPDATE})
@JsonIgnore
private Set<Course> courses = new HashSet<Course>();

字符串

相关问题