1.新建工程项目hibernate_one2one_ufk_1,添加hibernate所需Jar包和数据库驱动Jar包。
2.编写工具类HibernateUtils.java和ExportDB.java,代码如下:
HibernateUtils.java
- package com.i51pro.hibernate;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateUtils {
- private static SessionFactory sessionFactory;
- static {
- try {
- Configuration cfg = new Configuration().configure();
- sessionFactory = cfg.buildSessionFactory();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- public static Session getSession() {
- return sessionFactory.openSession();
- }
- public static void closeSession(Session session) {
- if (session != null) {
- if (session.isOpen()) {
- session.close();
- }
- }
- }
- }
ExportDB.java
- package com.i51pro.hibernate;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateUtils {
- private static SessionFactory sessionFactory;
- static {
- try {
- Configuration cfg = new Configuration().configure();
- sessionFactory = cfg.buildSessionFactory();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- public static Session getSession() {
- return sessionFactory.openSession();
- }
- public static void closeSession(Session session) {
- if (session != null) {
- if (session.isOpen()) {
- session.close();
- }
- }
- }
- }
3.编写实体类和映射文件,代码如下:
IdCard.java
- package com.i51pro.hibernate;
- public class IdCard {
- private int id;
- private String cardNo;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getCardNo() {
- return cardNo;
- }
- public void setCardNo(String cardNo) {
- this.cardNo = cardNo;
- }
- }
Person.java
- package com.i51pro.hibernate;
- public class Person {
- private int id;
- private String name;
- private IdCard idCard;
- 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;
- }
- public IdCard getIdCard() {
- return idCard;
- }
- public void setIdCard(IdCard idCard) {
- this.idCard = idCard;
- }
- }
IdCard.hbm.xml
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.i51pro.hibernate.IdCard" table="t_idcard">
- <id name="id">
- <generator class="native"/>
- </id>
- <property name="cardNo"/>
- </class>
- </hibernate-mapping>
Person.hbm.xml
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.i51pro.hibernate.Person" table="t_person">
- <id name="id">
- <generator class="native" />
- </id>
- <property name="name"/>
- <many-to-one name="idCard" unique="true" />
- </class>
- </hibernate-mapping>
4.编写hibernate映射文件,代码如下:
hibernate.cfg.xml
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_one2one_ufk_1</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">admin</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.show_sql">true</property>
- <mapping resource="com/i51pro/hibernate/Person.hbm.xml"/>
- <mapping resource="com/i51pro/hibernate/IdCard.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
5.在工程项目下新建SourceFolder,命名为test,在该包下编写单元测试类One2OneTest.java,进行单元测试,它的代码如下:
One2OneTest.java
- package com.i51pro.hibernate;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import junit.framework.TestCase;
- public class One2OneTest extends TestCase {
- public void testSave1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- Transaction tx = session.beginTransaction();
- IdCard idCard = new IdCard();
- idCard.setCardNo("88888888");
- Person person = new Person();
- person.setName("李小晚");
- person.setIdCard(idCard);
- //不能保存,因为idCard是Transident状态
- session.save(person);
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- public void testSave2() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- Transaction tx = session.beginTransaction();
- IdCard idCard = new IdCard();
- idCard.setCardNo("88888888");
- session.save(idCard);
- Person person = new Person();
- person.setName("李小晚");
- person.setIdCard(idCard);
- session.save(person);
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- public void testLoad1() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- Transaction tx = session.beginTransaction();
- Person person = (Person)session.load(Person.class, 2);
- System.out.println("person.name:" + person.getName() );
- System.out.println("idCard.name:" + person.getIdCard().getCardNo());
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }
- }
- }
总结:hibernate一对一唯一外键关联映射(单向关联Person---->IdCard)
一对唯一外键关联映射是多对一关联映射的特例
可以采用<many-to-one>标签,指定多的一端的unique=true,这样就限制了多的一端的多重性为一
通过这种手段映射一对一唯一外键关联