OneToManyToMany连续三个带有Hibernate Annotations的表

时间:2017-02-08 10:46:08

标签: java hibernate hibernate-mapping

我需要连续三个表与Hibernate Annotations连接。第二个表应该依赖于第一个表。然后第三个表应该依赖于第一个和第二个表。在结果中我应该有一些Java Map将其解析为json,就像这样。

    {
      "id": 203,
      "idGroup": 3,
      ......
      "subjects": [
        {
          "id": 121,
          "groups": 3,
          .....
          "studentsModel": null
          "marks": [
            {
              "id": 1,
              "id_person": 203,
              "id_subject": 121,
              "mark": 5
            }
          ]
        },
        {
          ........
        }
      ]
    }

我认为SQL会是这样的,但我不知道怎么用hibernate来做,但这个例子并不是不一致的,比如Json

SELECT * FROM students AS t1
INNER JOIN subjects t2 ON t2.groups = t1.id_group
INNER JOIN marks t3 ON (t3.id_subject=t2.id AND t3.id_stud=t1.id)

这里有一个连接的图像:

Schema of tables

我有Hibernate类:

学生实体:

            @Entity
            @Table(name = "students")
            public class StudentsModel implements Externalizable {
                @Id
                @Column(name = "id")
                @GeneratedValue(strategy = GenerationType.IDENTITY)
                private int id;               
                @Column(name = "id_group")
                private Integer idGroup;
                .......
                @OneToMany
                @JoinColumn(name="groups", referencedColumnName = "id_group")
                @OrderBy("name_of_subject")
                private Set<SubjectsModel> subjects;
                .......
            }

主题实体:

                @Entity
                @Table(name = "subjects")
                public class SubjectsModel implements Externalizable {
                    @Id
                    @GeneratedValue(strategy = GenerationType.IDENTITY)
                    private int id;
                    @Column(name = "groups")
                    private Integer groups;
                }

标记实体:

                @Entity
                @Table(name = "marks")

                public class MarksModel {
                    @Id
                    @GeneratedValue(strategy = GenerationType.AUTO)
                    @Column(name = "id")
                    private int id;
                    @Column(name = "mark")
                    private Integer mark;
                    @Column(name = "id_student")
                    private Integer idStud;
                    @Column(name = "id_subject")
                    private Integer idSubject;
                }

2 个答案:

答案 0 :(得分:0)

学生实体:

            @Entity
            @Table(name = "students")
            public class StudentsModel implements Externalizable {
                @Id
                @Column(name = "id")
                @GeneratedValue(strategy = GenerationType.IDENTITY)
                private int id;               
                @Column(name = "id_group")
                private Integer idGroup;
                .......
                @OneToMany
                @JoinColumn(name="id", referencedColumnName = "id_student")
                private Set<MarksModel> marks;
                .......
            }

主题实体:

                @Entity
                @Table(name = "subjects")
                public class SubjectsModel implements Externalizable {
                    @Id
                    @GeneratedValue(strategy = GenerationType.IDENTITY)
                    private int id;
                    @Column(name = "groups")
                    private Integer groups;
                    @OneToMany
                    @JoinColumn(name="id", referencedColumnName = "id_subject")
                    private Set<MarksModel> marks;


                }

标记实体:

                @Entity
                @Table(name = "marks")

                public class MarksModel {
                    @Id
                    @GeneratedValue(strategy = GenerationType.AUTO)
                    @Column(name = "id")
                    private int id;
                    @Column(name = "mark")
                    private Integer mark;
                    @ManyToOne(cascade = CascadeType.ALL)
                    @JoinColumn(name = "id_student")
                    private StudentsModel student;
                    @ManyToOne(cascade = CascadeType.ALL)
                    @JoinColumn(name = "id_subject")
                    private SubjectsModel subject;
                }

这就是映射应该是什么样子,因为你需要处理这些问题。如果订单具有相关性,请使用List代替Set。 然而,这并没有为您提供开箱即用的JSON结构 - 它实际上并不是由于数据库的构造方式。可能(?)有一些可以使它工作的JSON魔法,但我看到的唯一方法是通过另一组类进行序列化:

public class StudentPOJO implements Serializable {
    private String name;
    private int id;
    private List<SubjectPOJO> subjects = new ArrayList<>();

    public StudentPOJO(StudentsModel model) {
        this.name = model.getName();
        this.id = model.getId();
        this.subjects.addAll(model.getMarks().map(mark -> new SubjectPOJO(mark.getSubject())).collect(Collectors.toList()));
    }
}

,类似地为SubjectPOJO和MarkPOJO。

答案 1 :(得分:-1)

这些可能是商标类的一部分:

enter image description here