EJB notes three steps to success

Source: Internet
Author: User
Multiple-to-multiple relationships use the ing policy of the intermediate table connection. The created intermediate table introduces the primary keys on both sides as foreign keys.

@ Suppresswarnings ("serial ")
@ Entity
@ Table (name = "student ")
Public class student implements serializable ...{
Private integer studentid;
Private string studentname;
Private set <teacher> teachers = new hashset <teacher> ();

Public student ()...{}

Public student (string studentname )...{
Studentname = studentname;
}

@ ID
@ Generatedvalue
Public integer getstudentid ()...{
Return studentid;
}
Public void setstudentid (integer studentid )...{
This. studentid = studentid;
}

@ Column (nullable = false, length = 32)
Public String getstudentname ()...{
Return studentname;
}
Public void setstudentname (string studentname )...{
Studentname = studentname;
}

@ Manytomany (mappedby = "Students") // mappedby defines student as the maintenance end of the bidirectional relationship.
Public set <teacher> getteachers ()...{
Return teachers;
}
Public void setteachers (set <teacher> teachers )...{
This. Teachers = teachers;
}
}

 

 

@ Suppresswarnings ("serial ")
@ Entity
@ Table (name = "teacher ")
Public class teacher implements serializable ...{
Private integer teacherid;
Private string teachername;
Private set <student> students = new hashset <student> ();

Public Teacher ()...{}

Public Teacher (string teachername )...{
Teachername = teachername;
}

@ ID
@ Generatedvalue
Public integer getteacherid ()...{
Return teacherid;
}
Public void setteacherid (integer teacherid )...{
This. teacherid = teacherid;
}

@ Column (nullable = false, length = 32)
Public String getteachername ()...{
Return teachername;
}
Public void setteachername (string teachername )...{
Teachername = teachername;
}

@ Manytomany (cascade = cascadetype. persist, fetch = fetchtype. Lazy)
@ Jointable (name = "teacher_student ",
Joincolumns =... {@ joincolumn (name = "teacher_id", referencedcolumnname = "teacherid")}, // Foreign key of the maintained end
Inversejoincolumns =... {@ joincolumn (name = "student_id", referencedcolumnname = "studentid")}) // maintenance end foreign key
Public set <student> getstudents ()...{
Return students;
}
Public void setstudents (set <student> students )...{
This. Students = students;
}

Public void addstudent (Student )...{
If (! This. Students. Contains (student ))...{
This. Students. Add (student );
}
}

Public void removestudent (Student )...{
This. Students. Remove (student );
}
}

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.