Hibernate 一对一外键双向关联
一对一外键关联是一对多外键关联的特例,只是在多的一方加了个唯一性约束。
一、模型介绍
一个人(Person)对应一个地址(Address)。
二、实体(省略getter、setter方法)
public class Person11fkboth {
private int personid;
private String name;
private int age;
private Address11fkboth address11fkboth;
public class Address11fkboth {
private int addressid;
private String addressdetail;
private Person11fkboth person11fkboth;
三、表模型
mysql> desc address_11fkboth;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> desc person_11fkboth;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| addressId | int(11) | YES | UNI | NULL | |
+-----------+--------------+------+-----+---------+----------------+
四、生成的SQL脚本
CREATE TABLE `address_11fkboth` (
`addressid` int(11) NOT NULL auto_increment,
`addressdetail` varchar(255) default NULL,
PRIMARY KEY (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
CREATE TABLE `person_11fkboth` (
`personid` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`age` int(11) default NULL,
`addressId` int(11) default NULL,
PRIMARY KEY (`personid`),
KEY `FK68A8818F3F45AA77` (`addressId`),
CONSTRAINT `FK68A8818F3F45AA77` FOREIGN KEY (`addressId`) REFERENCES `address_11fk` (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
`addressid` int(11) NOT NULL auto_increment,
`addressdetail` varchar(255) default NULL,
PRIMARY KEY (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
CREATE TABLE `person_11fkboth` (
`personid` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`age` int(11) default NULL,
`addressId` int(11) default NULL,
PRIMARY KEY (`personid`),
KEY `FK68A8818F3F45AA77` (`addressId`),
CONSTRAINT `FK68A8818F3F45AA77` FOREIGN KEY (`addressId`) REFERENCES `address_11fk` (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;
五、映射方法:
OneToOneFKBothPerson.hbm.xml
<hibernate-mapping>
<class name="com.bean.Person11fkboth" table="PERSON_11fkboth">
<id name="personid" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="24" not-null="true">
<comment>姓名</comment>
</column>
</property>
<property name="age"/>
<one-to-one name="address11fkboth" cascade="all" />
</class>
</hibernate-mapping>
OneToOneFKBothAddress.hbm.xml
<hibernate-mapping>
<class name="com.bean.Address11fkboth" table="ADDRESS_11fkboth" catalog="HibernateMapping">
<id name="addressid" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="addressdetail" type="java.lang.String">
<column name="addressdetail" length="120" not-null="true">
<comment>详细地址</comment>
</column>
</property>
<many-to-one name="person11fkboth" class="com.bean.Person11fkboth" fetch="select" unique="true">
<column name="personid">
<comment>人的ID</comment>
</column>
</many-to-one>
</class>
</hibernate-mapping>
六、测试方法(双向的测试类都是JUnit单元测试的)
import org.junit.Test;
public class Test_11fkboth {
@Test
public void test_11fkboth() {
Person11fkboth person11fkboth = new Person11fkboth();
person11fkboth.setName("刘大");
person11fkboth.setAge(26);
Address11fkboth address11fkboth = new Address11fkboth();
address11fkboth.setAddressdetail("XX街X号");
person11fkboth.setAddress11fkboth(address11fkboth);
address11fkboth.setPerson11fkboth(person11fkboth);
Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction();
session.save(person11fkboth);
session.save(address11fkboth);
tx.commit();
HibernateUtil.closeSession();
}
}