Hibernate关系映射 --- 一对一实例分析

一  概念介绍

一对一的方式有两种,分为:

(1)基于主键的方式 --- 共用主键(Person --- IdCard)典型的一对一

    没有增加额外的列

(2)基于外键的方式 --- 增加额外的一列

下面介绍一下第一中基于主键的实现

二  代码分析

(1)Person类

package com.hbsi.domain;

public class Person {

    private int id;

    private String name;

    private IdCard idCard;//一个人对应一个IdCard

    public Person() {

       super();

       // TODO Auto-generated constructor stub

    }

    public Person(int id, String name, IdCard idCard) {

       super();

       this.id = id;

       this.name = name;

       this.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;

    }

    public String toString() {

       return "Person [id=" + id + ", name=" + name + ", idCard=" + idCard

              + "]";

    }

}

(2)IdCard类

package com.hbsi.domain;

import java.util.Date;

public class IdCard {

    private int id;

    private Date usefulLife;

    private Person person; //一个IdCard只属于一个人

    public IdCard() {

       super();

       // TODO Auto-generated constructor stub

    }

    public IdCard(int id, Date usefulLife, Person person) {

       super();

       this.id = id;

       this.usefulLife = usefulLife;

       this.person = person;

    }

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public Date getUsefulLife() {

       return usefulLife;

    }

    public void setUsefulLife(Date usefulLife) {

       this.usefulLife = usefulLife;

    }

 

    public Person getPerson() {

       return person;

    }

    public void setPerson(Person person) {

       this.person = person;

    }

}

(3)在配置文件中配置Person类和IdCard类

<!DOCTYPE hibernate-configuration PUBLIC

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<!-- 配置文件 -->

<hibernate-configuration>

    <session-factory>

       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

       <property name="hibernate.connection.url">jdbc:mysql:///demo</property>

       <property name="hibernate.connection.username">root</property>

       <property name="hibernate.connection.password">1234</property>

       <!-- 方言 针对哪个数据库Mysql -->

       <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

       <!-- 在程序运动的时候,增加自动创建表的属性,在程序终止 的时候销毁,但是在表格再次使用时,会重新建 -->

       <property name="hibernate.hbm2ddl.auto">update</property>

 

       <!-- 执行的sql语句显示出来 -->

       <property name="hibernate.show_sql">true</property>

       <!-- 指定映射文件的位置 -->

       <mapping resource="com/hbsi/domain/Person.hbm.xml" />

       <mapping resource="com/hbsi/domain/IdCard.hbm.xml" />

    </session-factory>

</hibernate-configuration>

相关推荐