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