One-to-one foreign key Association and one-to-one foreign key Association

Source: Internet
Author: User

One-to-one foreign key Association and one-to-one foreign key Association

Annotation: @ One2One @ JoinColumn
Xml: <role-to-one unique>

 

Generally, the foreign keys of the other table are designed in one table;

In the husb and table, the wife foreign key Association is designed.

Husband class:

1 package com. bjsxt. hibernate; 2 3 import javax. persistence. entity; 4 import javax. persistence. generatedValue; 5 import javax. persistence. id; 6 import javax. persistence. joinColumn; 7 import javax. persistence. oneToOne; 8 9 @ Entity10 public class Husband {11 12 private Integer id; 13 14 private String name; 15 16 private Wife wife; 17 18 @ Id19 @ GeneratedValue20 public Integer getId () {21 return id; 22} 23 24 public String getName () {25 return name; 26} 27 28 @ OneToOne // one-to-one relationship 29 @ JoinColumn (name = "wifeId") // Foreign key ID30 public Wife getWife () {31 return wife; 32} 33 34 public void setId (Integer id) {35 this. id = id; 36} 37 38 public void setName (String name) {39 this. name = name; 40} 41 42 public void setWife (Wife wife) {43 this. wife = wife; 44} 45}

Wife class

 1 package com.bjsxt.hibernate; 2  3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6  7 @Entity 8 public class Wife { 9     private Integer id;10     11     private String name;12 13     @Id14     @GeneratedValue15     public Integer getId() {16         return id;17     }18 19     public void setId(Integer id) {20         this.id = id;21     }22 23     public String getName() {24         return name;25     }26 27     public void setName(String name) {28         this.name = name;29     }30 }

Add the hibernate. cfg. xml configuration:

1 <mapping class="com.bjsxt.hibernate.Husband"/>2 <mapping class="com.bjsxt.hibernate.Wife"/>

SchemaExport code:

  @Test    public void testSchemaExport(){        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);    }

For example, the database automatically generates two tables and creates a foreign key association between the husb and table and the wife table.

 

Configure one-to-one association in XML:

Student and StudentIdCard)

Student

 1 package com.bjsxt.hibernate; 2  3 public class Student { 4      5     private Integer id; 6      7     private String name; 8      9     private Integer age;10     11     private String sex;12     13     private boolean good;14 15     public Integer getId() {16         return id;17     }18 19     public void setId(Integer id) {20         this.id = id;21     }22 23     public String getName() {24         return name;25     }26 27     public void setName(String name) {28         this.name = name;29     }30 31     public Integer getAge() {32         return age;33     }34 35     public void setAge(Integer age) {36         this.age = age;37     }38 39     public String getSex() {40         return sex;41     }42 43     public void setSex(String sex) {44         this.sex = sex;45     }46 47     public boolean isGood() {48         return good;49     }50 51     public void setGood(boolean good) {52         this.good = good;53     }54     55 }
View Code

StudentIdCard

 1 package com.bjsxt.hibernate; 2  3 public class StudentIdCard { 4  5     private Integer id; 6      7     private String num; 8      9     private Student student;10 11     public Integer getId() {12         return id;13     }14 15     public void setId(Integer id) {16         this.id = id;17     }18 19     public String getNum() {20         return num;21     }22 23     public void setNum(String num) {24         this.num = num;25     }26 27     public Student getStudent() {28         return student;29     }30 31     public void setStudent(Student student) {32         this.student = student;33     }34     35 }
View Code

The student object is added to StudentIdCard and configured in StudentIdCard. hbm. xml for one-to-one association:

1 <many-to-one name="student" column="studentId" unique="true"></many-to-one>

Required jar package link: https://pan.baidu.com/s/1i5fxsjF password: qw9u

Code link: https://pan.baidu.com/s/1dFzin3z password: b7e2

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.