Mappedby
We know that Mappedby is used to specify two entities that have a bidirectional relationship. Which entity is being associated with. It has the following four features:
1. The Mappedby attribute is only available on Onetoone,onetomany,manytomany, Manytoone does not exist;
[Email protected] (mapped= "is a party to the many by one party, and this attribute should be equal to the name of the object in the many party that contains the property of the one class, otherwise an error")
3. The owner of the relationship (i.e. the many party) is responsible for the maintenance of the relationship and establishes the foreign key in the owning party. So use the @joincolumn.
4.mappedBy and joincolumn/jointable are always on the side of mutual exclusion.
Comparison of Mappedby and @joincolumn
However, when using @onetomany annotations, in order for one to know which field of an object in many to associate with itself, on the many side, we can point to an object variable with @onetomany mappedby. You can also use @joincolumn to point to one of the foreign key fields.
For example: There are multiple students in a grade, which is obviously: grade (one) student (many)
Student (Manytoone):
1 // Student 2 Public class Student { 3 4 @ManyToOne 5 @ Joincolumn (name = "grade_id") 6 private grade grade; 7 8 // get and Set methods omitted 9 }
Grade (Onetomany):
Mappedby Way:
1 // Grade 2 public class Grade { 3 4 @OneToMany (Mappedby = "Grade", Cascade = Cascadetype.all) 5 private set<student> students; 6 7 get and set way omitted 8 }
The above grade uses the @onetomany annotation to specify the objects in the collection by Mappedby student is associated to grade through the grade attribute.
Cascade is also set to Cascadetype.all, and when an action is performed in a grade, the same action is cascaded on the student in the collection, It is important to note that the Garde property must be set for student in the collection, which is the grade of the current operation, otherwise the foreign key of the new student will be empty.
@JoinColumn Way:
1 public class Grade { 2 3< /span> @OneToMany (Cascade = Cascadetype.all) 4
@JoinColumn (name = "grade_id" ) 5 private set<student> students; 6 7 get and set way omitted 8 }
The above grade is specified by declaring @joincolumn in the collection student through the foreign key grade_id associated to the current grade, when set cascade, When doing some sort of operation on a grade, the same action is performed on the student in the collection, and an update is made to the new object in the collection to ensure that the object in the collection has a foreign key key to the current object. So you don't have to set student to point to grade.
It is important to note that if you do not set a cascade, but there are unsaved student in the collection, the exception is reported when you operate on grade, Org.springframework.dao.InvalidDataAccessApiUsageException: Object references an unsaved transient instance-save the transient instance before
When cascading, it is equivalent to issuing the following update statement to maintain the object and one relationship in many:
Update T_student Set grade_id=? where ID=?
Looking at the data, it may be better to use mappedby because there is no more UPDATE statement to maintain the object and one relationship in many when cascading. But it has to be selected according to the actual situation.
In Hibernate, the comparison of Mappedby and annotated @joincolumn