说明:
一、还是举那个一夫一妻的例子来讲述一对一单向主键关联。
二、本次使用的Hibernate版本是hibernate-release-4.3.11.Final.
先写Annotation版本:
实体类:
1.Husband.java
package com.buaa.hibernate.bean; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.MapsId; import javax.persistence.OneToOne; @Entity public class Husband { private int id; private String name; private Wife wife; @Id 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; } @OneToOne @MapsId //4.3.11版本的Hibernate将注解PrimaryKeyJoinColumn→MapsId public Wife getWife() { return wife; } public void setWife(Wife wife) { this.wife = wife; } }Wife.java:
package com.buaa.hibernate.bean; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Wife { private int id; private String name; @Id @GeneratedValue 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; } }
xml版本:
Husband.hbm.xml:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.buaa.hibernate.bean"> <class name="Husband" table="HUSBAND"> <id name="id" column="wifeId"> <generator class="foreign"> <param name="property">wife</param> </generator> </id> <property name="name"/> <one-to-one name="wife" constrained="true"/> </class> <class name="Wife"> <id name="id"> <generator class="native" /> </id> <property name="name"/> </class> </hibernate-mapping>此处将两个类写在一个配置文件里面了,并且配置文件里面的<one-to-one/>元素属性constrained一定要设置为true,如果不设置的话,将不会产生外键关联。
配置文件hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- Assume test is the database name --> <property name="connection.url">jdbc:mysql://localhost/test</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!--<property name="hbm2ddl.auto">create</property>--> <!-- <mapping resource="com/buaa/hibernate/bean/Husband.hbm.xml"/> --> <mapping class="com.buaa.hibernate.bean.Husband"/> <mapping class="com.buaa.hibernate.bean.Wife"/> </session-factory> </hibernate-configuration>
下面是Junit测试类:
package com.buaa.hibernate.bean; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.Test; public class OneToOneTest { @Test public void test(){ Wife wife = new Wife(); wife.setName("lisa"); Husband t = new Husband(); t.setName("james"); t.setWife(wife); Configuration cfg = new Configuration().configure(); ServiceRegistry service = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build(); SessionFactory factory = cfg.buildSessionFactory(service); Session session = factory.openSession(); session.beginTransaction(); session.save(wife); session.save(t); session.getTransaction().commit(); session.close(); factory.close(); } @Test public void SchemaExportTest(){ Configuration cfg = new Configuration().configure(); //第一个参数是否将sql语句输出到控制台,第二个参数是否在数据库中生成对应的表格 new SchemaExport(cfg).create(true, true); } }