On @mappedBy meaning of Hibernate Annotation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.