A one-to-many/many-to-one is the most common relationship in a database table relationship. Two tables are associated with a foreign key to express a "one-to-many/many-to-a" relationship. Foreign keys are usually located in the "multiparty" table.
Use the example of a student's elective course to understand the @mappedby of this annotation.
Relationship:
Students student multi---------Courses course multi-party
Students student multi---------teacher Teacher Party
Mappedby points to a party that abandons the maintenance relationship. The student and the curriculum, namely the course class abandons the maintenance relation, the foreign key is in the student class. Students and teachers, that is, teachers teacher give up maintenance relations, foreign key in the student student class.
@Entity @table (name= "T_student") Public classStudent {@Id @GeneratedValuePrivateInteger ID;//primary Key ID PrivateString uname;//Student Name PrivateString pwd;//Password PrivateString phone;//Mobile phone number PrivateString email;//Email /** Foreign keys are usually placed in a multi-party table and placed in the Student table. Course Abandon Maintenance*/@ManyToManyPrivateset<course> courses =NewHashset<course>(); @ManyToOnePrivateTeacher Teacher;
@Entity @table (name = "T_course" ) Span style= "color: #0000ff;" >public class Course {@Id @GeneratedValu E private Integer ID; private String Coursedesc; // course description private String Courseno; // course code // course abandons the maintenance foreign key. @ManyToMany (mappedby= "courses" ) set<student>students= new hashset<student> ();
@Entity @table (name= "T_teacher") Public classTeacher {@Id @GeneratedValuePrivateInteger ID; PrivateString name;//name PrivateString pwd;//Password PrivateString phone;//Mobile Phone /** Foreign keys are generally placed in multiparty tables. * Mappedby points to classes that give up maintenance relationships. That is, the Teacher class abandons the maintenance relationship, and the foreign key is placed in the student class for students to maintain. */@OneToMany (Mappedby= "Teacher") PrivateSet<student>students =NewHashset<student> ();
Database forms
Course Class Form
Student class Forms
Student-Teacher Relationship form
On @mappedBy meaning of Hibernate Annotation