In the all the ORM tool hibernate is mostly used in the IT.
In the Hibernate we can also implement data base relation ship like one-to-one, one-to-many,many-to-one. Here i'm going to explain one-to-many relationship using hibernate annotation.
According to the relationship a student can attend any number of courses.
To create this relationship we need to have a STUDENT and COURSE table. The relational model shown below-
We can create this relation in two ways -
- we will create a column in the course table which will have student id for that course.
- we will create a intermediate table which has course id and student id.
Here we discuss by creating a column in the course(Child Table)table. This column shows the one to many relationship between parent (Student) table and child (Course) table. This column can have same value for multiple records of the course table because a student id can be belongs to the multiple course to show which are courses are applied by a particular student. One more important thing is the studentId should be the primary key of the student table.
First we will a Student class which will be responsible for the student table. In the Student table we have following field like studentId,studentName and we have one more field of Set of course. This field doesn't have any mapped column but this field is required to make one to many relationship. This field have all the course detail of a student for which he/she applied.
Here we used some annotation like @table, @column,@JoinColumn etc. We will discuss later.
In the Student class, we have mapping of different field with the different columns.
- Student class mapped to the STUDENT TABLE.
- studentId Mapped to STUDENT_ID column.
- studentName mapped to student_name column.
- courses didn't mapped to any column, but we are using @OneToMany annotation any showing that the join column is STD_ID.
Now We need a Course table. We will create a with the name of Course. Course class is responsible for the COURSE table. COURSE Table we have 2 column course_ID and course_name. our course class will be look like
- Course class mapped to the COURSE TABLE.
- courseId Mapped to course_ID column.
- courseName mapped to course_name column.
As of now we used 4-5 annotations now its time get understand about these annotation
@Table used to map with a class to a Database Table
@Id shows this the id for the table.
@GeneratedValue shows the value of Id should be generate by the database itself.
@Column used to map a field to database column.
@OneToMany shows the one-to-many relationship between two tables.
@JoinColumn which join column used to make the relationship.
@Cascade which action should be taken for the child table depend on the parent table action like delete, update.
Till now we created both table STUDENT and Course. Now we have to write a class which can use hibernate to create the table and insert the record.
After executing above code we will 2 tables in the hibernate as below
We can see that one extra column with the name of STUDENT_ID is exist in the Course Table, if we look at the code of course class there is no field with name of Student_id.In the Student table we have a annotation @joinColumn, this annotation is responsible for this extra column in the course table. There is issue in the above implemention. After getting the course Object through hibernate, we cann't get the which studentId is Responsible for this course record. Other issue is we cann't find the Course object through student Id because while executing a query there is no field in the course class which map to studentId To overcome this problem we have to add below statement in the course class