妻子和丈夫: 在丈夫表里面有妻子的外键
学生和学生证: 在学生证表里面有学生的外键
第一种:Annotation配置
package com.zchen.hibernate.oneTone; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name="T_HUSBAND") public class Husband { private int id; private String name; private Wife wife; @OneToOne @JoinColumn(name="WIFE_ID") public Wife getWife() { return wife; } public void setWife(Wife wife) { this.wife = wife; } @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.zchen.hibernate.oneTone; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name="T_WIFE") public class Wife { private int id; private String name; private Husband husband; @OneToOne(mappedBy="wife") public Husband getHusband() { return husband; } public void setHusband(Husband husband) { this.husband = husband; } @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
第二种:xml配置
package com.zchen.hibernate.oneTone; public class Student { private int id; private String name; private StudentIdCard stuIdCard; public StudentIdCard getStuIdCard() { return stuIdCard; } public void setStuIdCard(StudentIdCard stuIdCard) { this.stuIdCard = stuIdCard; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.zchen.hibernate.oneTone; public class StudentIdCard { private int id; private String name; private Student student; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.zchen.hibernate.oneTone"> <class name="StudentIdCard" table="T_STUIDCARD"> <id name="id" column="ID"> <generator class="native"/> </id> <property name="name" column="NAME"/> <many-to-one name="student" column="STUID" unique="true"></many-to-one> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.zchen.hibernate.oneTone"> <class name="Student" table="T_STU"> <id name="id" column="ID"> <generator class="native"/> </id> <property name="name" column="NAME"/> <one-to-one name="stuIdCard" property-ref="student"></one-to-one> </class> </hibernate-mapping>