2

I need to connect successively three tables with Hibernate Annotations.The second table should be dependent of the first. Then the third table should be dependent on the first and second tables. And in the result I should have some Java Map for parsing it into the json like this.

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

I think SQL will be like this, but I don't know how to do it with hibernate, but this example is not in inconsistent manner, like 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)

Here there is an image of this connecting:

Schema of tables

I have Hibernate classes:

Students entity:

            @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;
                .......
            }

Subjects entity:

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

Marks entity:

                @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;
                }
Alex Ul
  • 19
  • 7
  • Your mapping between Student and Subject is not really a one to many. It's Many to Many with Marks serving as a join table with extra information. I'll try and answer in an hour or two if I have the time (and you don't find a solution yourself meantime) – Deltharis Feb 08 '17 at 11:45
  • Thanks so much, nice to hear)) – Alex Ul Feb 08 '17 at 11:47

2 Answers2

0

Students entity:

            @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;
                .......
            }

Subjects entity:

                @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;


                }

Marks entity:

                @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;
                }

That's how the mapping should look like, for all your JPA needs of handling those. If order will be relevant use List instead of Set. This however does not give you the JSON structure you dream of out of the box - it can't really due to how the database is constructed. There might (?) be some JSON magic to be done that could make it work, but the only way I see it is through another set of classes for serialization purposes:

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()));
    }
}

and analogically for SubjectPOJO and MarkPOJO.

Deltharis
  • 2,320
  • 1
  • 18
  • 29
-1

These could be part of marks class:

enter image description here

  • These files specify the relationship between your beans. – uheredero001 Feb 08 '17 at 10:58
  • But I need use only annotations(( – Alex Ul Feb 08 '17 at 10:59
  • I don´t understand you when you say annotations. ¿Could you please put an example in a comment? – uheredero001 Feb 08 '17 at 11:02
  • @OneToMany @JoinColumn(name="groups", referencedColumnName = "id_group") @OrderBy("name_of_subject") private Set subjects; – Alex Ul Feb 08 '17 at 11:03
  • Like this example it works with two tables but i don't know about depending of the third table which is dependent of the first and second tables – Alex Ul Feb 08 '17 at 11:04
  • Watch the examples I put, there are some one to many relationships. – uheredero001 Feb 08 '17 at 11:04
  • You have sent me with xml – Alex Ul Feb 08 '17 at 11:05
  • You only have to do a relationship between student and marks, and another between marks and subject, where the relationship id is the other class key. I don´t know if you understand what I´m saying. – uheredero001 Feb 08 '17 at 11:07
  • Ok. You only have to put another relationship in marks because subject depends of student and marks will depend of subject and student. – uheredero001 Feb 08 '17 at 11:09
  • How can i add it to the subject respectively? – Alex Ul Feb 08 '17 at 11:10
  • In subject: the relationship is many subject to one student and one subject to many marks. – uheredero001 Feb 08 '17 at 11:20
  • Can you put it with example, because I think you don't understand what I want, have you seen picture? – Alex Ul Feb 08 '17 at 11:21
  • May be this is what you want. In subject: the relationship is many subject to one student and one subject to many marks. In marks: one mark owns to one student and many marks owns to one subject. – uheredero001 Feb 08 '17 at 11:23
  • Yee, that's right)) But how to do it with annotations, do you know? I think it should be in one class. – Alex Ul Feb 08 '17 at 11:24
  • Well you could do it in your classes. In each class put the relationships but you have to be careful because you have to put the same in the other class. For example: relationship student subject: in student: one student to many subject in subject: many subject to one student You have to be coherent – uheredero001 Feb 08 '17 at 11:31
  • Now it works with subject, I need only add marks to student, see my example – Alex Ul Feb 08 '17 at 11:33
  • Ok. Many marks are related to one student, so put that relationship in marks and the oposite (one student is related to many marks) in student. But you have to put also that many marks are related to one subject and the oposite in subject. Do you understand me? – uheredero001 Feb 08 '17 at 11:36
  • @uheredero001 if you think you can actually answer the question, please edit your answer. As it is, it is completely irrelevant. – Deltharis Feb 08 '17 at 11:38
  • show this: https://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/ – uheredero001 Feb 08 '17 at 11:39
  • And again - your answer doesn't really answer the question. And is slightly wrong. Also, please in the future use the editor instead of screenshots, code being copyable is important ease-of-use feature of Stack Overflow. – Deltharis Feb 08 '17 at 13:20