一个男人对应一个女人,一个女人只能属于一个男人
方式一:外键映射
Male类
public class Male implements Serializable { private Integer id; private String name; private Integer age; private Female female; }
Male.hbm.xml,在male表中引入外键列female_id
<?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"> <hibernate-mapping> <class name="rock.lee.bean.Male" table="male" catalog="test"> <id name="id" column="id" type="int"> <generator class="native"></generator> </id> <property name="name" column="name" type="java.lang.String"></property> <property name="age" column="age" type="int"></property> <many-to-one name="female" class="rock.lee.bean.Female" unique="true" column="female_id"></many-to-one> </class> </hibernate-mapping>
对于基于外键的1-1关联,其外键可以存放在任意一边,在需要存放外键一端,增加 many-to-one 元素。为 many-to-one元素增加 unique=“true” 属性来表示为1-1关联,并用name属性来指定关联属性的属性名
Female类
public class Female implements Serializable { private Integer id; private String name; private Integer age; private Male male; }
Female.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"> <hibernate-mapping> <class name="rock.lee.bean.Female" table="female" catalog="test"> <id name="id" column="id" type="int"> <generator class="native"></generator> </id> <property name="name" column="name" type="java.lang.String"></property> <property name="age" column="age" type="int"></property> <one-to-one name="male" class="rock.lee.bean.Male" property-ref="female"></one-to-one> </class> </hibernate-mapping>
另一端需要使用one-to-one元素,该元素使用 property-ref 属性指定使用被关联实体主键以外的字段作为关联字段,也就是指定female在male表中的外键
在hibernate.cfg.xml中配置加载hbm文件
<mapping resource="rock/lee/bean/Male.hbm.xml" /> <mapping resource="rock/lee/bean/Female.hbm.xml" />
测试保存
@Test public void testOntToOne01() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Male male = new Male("小明", 20); Female female = new Female("孙艺珍", 33); male.setFemale(female); female.setMale(male); session.save(male); session.save(female); transaction.commit(); session.close(); }
测试惟一性:
mysql> select * from female; +----+--------+------+ | id | name | age | +----+--------+------+ | 1 | 孙艺珍 | 33 | +----+--------+------+ 1 row in set (0.00 sec) mysql> select * from male; +----+------+------+-----------+ | id | name | age | female_id | +----+------+------+-----------+ | 1 | 小明 | 20 | 1 | +----+------+------+-----------+ 1 row in set (0.00 sec)
小明和孙艺珍离婚了,娶了孙艺珍的闺蜜林允儿
@Test public void testOntToOne02() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Male male = (Male) session.get(Male.class, 1);//查询小明 Female female = new Female("林允儿", 26); male.setFemale(female); session.save(male); session.save(female); transaction.commit(); session.close(); }
小明再婚成功
mysql> select * from female; +----+--------+------+ | id | name | age | +----+--------+------+ | 1 | 孙艺珍 | 33 | | 2 | 林允儿 | 26 | +----+--------+------+ 2 rows in set (0.00 sec) mysql> select * from male; +----+------+------+-----------+ | id | name | age | female_id | +----+------+------+-----------+ | 1 | 小明 | 20 | 2 | +----+------+------+-----------+ 1 row in set (0.00 sec)
几年后,林允儿看上了宋祖德,林允儿即不想离婚,又想和宋祖德在一起
@Test public void testOntToOne03() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Female female = (Female) session.get(Female.class, 2); Male male = new Male("宋祖德",55); male.setFemale(female); session.save(male); session.save(female); transaction.commit(); session.close(); }
会抛出异常
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'female_id'
可以对感情不忠,但不能重婚
逗逼一下:
@Test public void testOntToOne04() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Female female = (Female) session.get(Female.class, 1); Male male = new Male("博主",27); male.setFemale(female); session.save(male); session.save(female); transaction.commit(); session.close(); }
You To Me & Me To You Ⅰ
mysql> select * from female; +----+--------+------+ | id | name | age | +----+--------+------+ | 1 | 孙艺珍 | 33 | | 2 | 林允儿 | 26 | +----+--------+------+ 2 rows in set (0.00 sec) mysql> select * from male; +----+------+------+-----------+ | id | name | age | female_id | +----+------+------+-----------+ | 4 | 博主 | 27 | 1 | +----+------+------+-----------+ 1 row in set (0.00 sec)
永远的初恋情人
方案二:主键映射
female表的主键是引用male的主键
Male.hbm.xml
<hibernate-mapping> <class name="rock.lee.bean.male" table="male"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <one-to-one name="female" class="rock.lee.bean.Female"></one-to-one> </class> </hibernate-mapping>
Female.hbm.xml
<hibernate-mapping> <class name="rock.lee.bean.Female" table="female"> <id name="id"> <generator class="foreign"> <param name="property">company</param> </generator> </id> <property name="info"></property> <one-to-one name="male" class="rock.lee.bean.Male" constrained="true"></one-to-one> </class> </hibernate-mapping>
基于主键的映射策略:指一端的主键生成器使用 foreign 策略,表明根据”对方”的主键来生成自己的主键,自己并不能独立生成主键. <param> 子元素指定使用当前持久化类的那个属性作为 “对方”
采用foreign主键生成器策略的一端增加 one-to-one 元素映射关联属性,其 one-to-one 属性还应增加 constrained=“true” 属性;另一端(company)增加one-to-one元素映射关联属性。
constrained(约束):指定为当前持久化类对应的数据库表的主键添加一个外键约束,引用被关联的对象(“对方”)所对应的数据库表主键