![](http://dl.iteye.com/upload/attachment/592506/f1435ab1-aead-3083-b063-d6245c0a8e70.jpg)
stuidcard表结构
![](http://dl.iteye.com/upload/attachment/592508/95ccf1ae-111d-360d-b787-86854052bda0.jpg)
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; } }
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; } }
<?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> <!-- property-ref 与 jpa中的mappedBy 同理, 关联关系由StuIdCard中的student确定 --> <one-to-one name="stuIdCard" property-ref="student"></one-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> <class name="com.bjsxt.hibernate.StuIdCard"> <id name="id"> <generator class="foreign"> <param name="property">student</param> </generator> </id> <property name="num"/> <one-to-one name="student" constrained="true"></one-to-one> </class> </hibernate-mapping>