凡是双向关联必设mapped by 由对方主导
wifi.java
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity 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; } }
Husband.java
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; @Entity public class Husband { private int id; private String name; private Wife wife; @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } @OneToOne @JoinColumn(name="wifeId") public Wife getWife() { return wife; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setWife(Wife wife) { this.wife = wife; } }
测试类略
XML中
Student.java
package com.bjsxt.hibernate; public class Student { private int id; private String name; private int age; private String sex; private boolean good; private StuIdCard stuIdCard; public StuIdCard getStuIdCard() { return stuIdCard; } public void setStuIdCard(StuIdCard stuIdCard) { this.stuIdCard = stuIdCard; } public boolean isGood() { return good; } public void setGood(boolean good) { this.good = good; } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
StuidCard.java
package com.bjsxt.hibernate; public class StuIdCard { private int id; private String num; private Student student; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
Student.hbm.xml
<?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> <class name="com.bjsxt.hibernate.Student" dynamic-update="true"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="age" /> <property name="sex" /> <property name="good" type="yes_no"></property> <one-to-one name="stuIdCard" property-ref="student"></one-to-one> </class> </hibernate-mapping>
Stuidcard.xml
<?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> <class name="com.bjsxt.hibernate.StuIdCard"> <id name="id"> <generator class="native"></generator> </id> <property name="num"/> <many-to-one name="student" column="studentId" unique="true"></many-to-one> </class> </hibernate-mapping>
测试类代码略
单向 双向在数据库中实际山没区别,但利于编程
一对一单向主键关联(不重要)
Xml配置
略