一对一关联有两种实现方式:一种是共享的主键关联,另一种是一对一的外键关联
1、共享的主键关联:让两个对象具有共同的主键值,以表明他们之间的一一对应关系。
Person.java类
public class Person implements java.io.Serializable { // Fields private Integer id;
private String name;
private Idcard idcard; // Constructors /** default constructor */
public Person() {
} /** full constructor */
public Person(String name) {
this.name = name;
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
public Idcard getIdcard() {
return idcard;
} public void setIdcard(Idcard idcard) {
this.idcard = idcard;
}
}
Idcard.java类
public class Idcard implements java.io.Serializable { // Fields private Integer id;
private String idcardno;
private Person person; // Constructors /** default constructor */
public Idcard() {
} /** full constructor */
public Idcard(String idcardno) {
this.idcardno = idcardno;
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getIdcardno() {
return this.idcardno;
} public void setIdcardno(String idcardno) {
this.idcardno = idcardno;
} public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} }
Person.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.db.entity.Person" table="person" catalog="mydb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="30" />
</property>
<one-to-one name="idcard" class="com.db.entity.Idcard" cascade="all" />
</class>
</hibernate-mapping>
Idcard.hbm.xml
<hibernate-mapping>
<class name="com.db.entity.Idcard" table="idcard" catalog="mydb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" constrained="true" class="com.db.entity.Person"></one-to-one>
<property name="idcardno" type="java.lang.String">
<column name="idcardno" length="20" />
</property>
</class>
</hibernate-mapping>
bibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/mydb
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
MyDBAccount
</property>
<mapping resource="com/db/entity/Idcard.hbm.xml" />
<mapping resource="com/db/entity/Person.hbm.xml" />
</session-factory> </hibernate-configuration>
测试代码:
public class TestOneOne { public static void main(String[] args) {
// TODO Auto-generated method stub Session session=null;
session=HibernateSessionFactory.getSession();
session.beginTransaction();
Idcard idcard=new Idcard();
idcard.setIdcardno("2345678901");
Person person=new Person();
person.setName("Lily");
person.setIdcard(idcard);
idcard.setPerson(person);
session.save(person);
session.save(idcard);
session.getTransaction().commit(); } }
2、一对一的外键关联:外键关联,本来是用于多对一的配置,但是加上唯一的限制之后(采用<many-to-one>标签来映射,指定多的一端unique为true,这样就限制了多的一端的多重性为一),也可以用来表示一对一关联关系,其实它就是多对一的特殊情况。
首先在表idcard中新添加一列pid用于共享数据
Person.java类
public class Person implements java.io.Serializable { // Fields private Integer pid;
private String name;
private Idcard idcard; // Constructors /** default constructor */
public Person() {
} /** full constructor */
public Person(String name) {
this.name = name;
} // Property accessors public Integer getPid() {
return this.pid;
} public void setPid(Integer pid) {
this.pid = pid;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
}
public Idcard getIdcard() {
return idcard;
} public void setIdcard(Idcard idcard) {
this.idcard = idcard;
}
}
idcard.java类
public class Idcard implements java.io.Serializable { // Fields private Integer iid;
private String idcardno;
private Person person; // Constructors /** default constructor */
public Idcard() {
} /** full constructor */
public Idcard(String idcardno) {
this.idcardno = idcardno;
} // Property accessors public Integer getIid() {
return this.iid;
} public void setIid(Integer iid) {
this.iid = iid;
} public String getIdcardno() {
return this.idcardno;
} public void setIdcardno(String idcardno) {
this.idcardno = idcardno;
} public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} }
Person.hbm.xml
<hibernate-mapping>
<class name="com.db.entity.Person" table="person" catalog="mydb">
<id name="pid" type="java.lang.Integer">
<column name="pid" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="30" />
</property>
<one-to-one name="idcard" class="com.db.entity.Idcard" property-ref="person"></one-to-one>
</class>
</hibernate-mapping>
Idcard.hbm.xml
<hibernate-mapping>
<class name="com.db.entity.Idcard" table="idcard" catalog="mydb">
<id name="iid" type="java.lang.Integer">
<generator class="native">
</generator>
</id>
<many-to-one name="person" column="pid" class="com.db.entity.Person"
cascade="save-update" unique="true"></many-to-one>
<property name="idcardno" type="java.lang.String">
<column name="idcardno" length="20" />
</property>
</class>
</hibernate-mapping>
bibernate.cfg.xml文件和测试代码同上。
如果使用注解映射外键,则注解如下:
Idcard.java类
public class Idcard implements java.io.Serializable { // Fields
private Person person; @OneToOne
@JoinColumn(name="pid")
public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} }
Person.java类
public class Person implements java.io.Serializable { // Fields
private Idcard idcard; @OneToOne(mappedBy="person")
public Idcard getIdcard() {
return idcard;
} public void setIdcard(Idcard idcard) {
this.idcard = idcard;
}
}