hibernate一对一单向外键关联
hibernate一对一单向外键关联
一、场景
我国实行一夫一妻制,一个Husband对应一个Wife,其中Wife为主控方(悲剧啊),即wife是属于one的一方
二、R
?
?
CREATE TABLE `wife` (
? `id` int(11) NOT NULL AUTO_INCREMENT,
? `name` varchar(50) NOT NULL,
? PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
?
?
CREATE TABLE `husband` (
? `id` int(11) NOT NULL AUTO_INCREMENT,
? `name` varchar(50) NOT NULL,
? `wifeid` int(11) DEFAULT NULL,
? PRIMARY KEY (`id`),
? KEY `fk_wife` (`wifeid`),
? CONSTRAINT `fk_wife` FOREIGN KEY (`wifeid`) REFERENCES `wife` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
三、O
?
?
Wife
?
?
package com.linys.model;/** * Wife entity. @author MyEclipse Persistence Tools */public class Wife implements java.io.Serializable {// Fields/** * */private static final long serialVersionUID = 1L;private Integer id;private String name;// Constructors/** default constructor */public Wife() {}/** minimal constructor */public Wife(String name) {this.name = name;}// Property accessorspublic 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;}}
?
?Husband
?
package com.linys.model;/** * Husband entity. @author MyEclipse Persistence Tools */public class Husband implements java.io.Serializable {// Fields/** * */private static final long serialVersionUID = 1L;private Integer id;private Wife wife;private String name;// Constructors/** default constructor */public Husband() {}/** minimal constructor */public Husband(String name) {this.name = name;}/** full constructor */public Husband(Wife wife, String name) {this.wife = wife;this.name = name;}// Property accessorspublic Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public Wife getWife() {return this.wife;}public void setWife(Wife wife) {this.wife = wife;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}}?
?
四、映射文件
Wife.hbm.xml
?
?
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.linys.model.Wife" table="wife"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator /> </id> <property name="name" type="java.lang.String"> <column name="name" length="50" not-null="true" /> </property> </class></hibernate-mapping>
?
?Husband.hbm.xml
?
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.linys.model.Husband" table="husband"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator /> </id> <property name="name" type="java.lang.String"> <column name="name" length="50" not-null="true" /> </property> <many-to-one name="wife" unique="true" > <column name="wifeid" /> </many-to-one> </class></hibernate-mapping>
?
?其中:
<many-to-one:one-to-one是many-to-one的特殊一种,只是将unique 设置成true即可
many-to-one可以看做是foreign key字段的映射,
五、测试程序
?
?
package com.linys.model;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;public class TestOne2OneUniFK {static SessionFactory sf;@BeforeClasspublic static void setUpBeforeClass() throws Exception {sf = new Configuration().configure().buildSessionFactory();}@Testpublic void testOne2OneUniFK(){Wife wife=new Wife();wife.setName("wife");Husband husband=new Husband();husband.setName("husband");husband.setWife(wife);Session session=sf.openSession();Transaction ts=session.beginTransaction();//插入可以//session.save(wife);//session.save(husband);//插入可以//session.save(husband);//session.save(wife);//插入不行// session.save(husband);//插入可以session.save(wife);ts.commit();}@AfterClasspublic static void tearDownAfterClass() throws Exception {sf.close();}}?
?
?
?