Chapter 10 annotation-based relationship ing

Source: Internet
Author: User

If the following content does not understand, you can find it:

Annotation-based relationship ing preparations: Http://blog.csdn.net/p_3er/article/details/9061911

XML-based many to one: http://blog.csdn.net/p_3er/article/details/9036759

XML-based one-to-many: http://blog.csdn.net/p_3er/article/details/9036921

This article combines many-to-one with one-to-many to form a bidirectional ing. If you only want one way, just remove the annotations on the other side.

 

Department:
@ Entity @ table (name = "department", catalog = "hibernate") public class Department implements Java. io. serializable {private integer ID; private string name; private set <employee> employees = new hashset <employee> (0); public department () {} public department (string name, set <employee> employees) {This. name = Name; this. employees = employees;} @ ID @ generatedvalue @ column (name = "ID", unique = true, nullable = False) Public integer GETID () {return this. ID;} public void setid (integer ID) {This. id = ID ;}@ column (name = "name", nullable = false, length = 45) Public String getname () {return this. name;} public void setname (string name) {This. name = Name ;}@ onetomany (cascade = cascadetype. all, fetch = fetchtype. lazy, mappedby = "Department")/* One to multiple. In the one-to-multiple relationship between Department and employee, when mappedby in department is specified, the relationship can only be actively maintained by employee. that is, the processing department of the employee cascade. previous ing files: <set name = "employees" inverse = "false" cascade = "all"> <key column = "department_id"> </key> <one-to-learn class = "cn. framelife. hibernate. entity. employee "/> </set> */Public set <employee> getemployees () {return this. employees;} public void setemployees (set <employee> employees) {This. employees = employees ;}}

 

Employee:
 
@ Entity @ table (name = "employee", catalog = "hibernate") public class employee implements Java. io. serializable {private integer ID; private department Department; private string name; public employee () {}@ ID @ generatedvalue @ column (name = "ID", unique = true, nullable = false) Public integer GETID () {return this. ID;} public void setid (integer ID) {This. id = ID;} @ manytoone (fetch = fetchtype. lazy) @ joincolumn ( Name = "department_id")/* multiple to one. <Role-to-one name = "department" column = "department_id"> </role-to-one> */public department getdepartment () {return this. department;} public void setdepartment (Department) {This. department = Department;} @ column (name = "name", nullable = false, length = 45) Public String getname () {return this. name;} public void setname (string name) {This. name = Name ;}}

 

 

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.