Hibernate关联映射

映射关联关系

关联关系分类:

  1. 单向关联
  2. 双向关联

映射关联关系就是把对象模型中类之间的关联关系映射成关系模型中表之间的外键引用关系


一对一关联映射策略

  1. 外键关联
  2. 主键关联


基于外键的单向一对一关联映射

基于外键的单向一对一实际上是多对一关联映射的特例

采用<many-to-one>标签,指定多的一段的unique=true,这样就限制了多段的多重性为一


Account.java

package entity;

import com.sun.corba.se.impl.protocol.AddressingDispositionException;

import java.io.Serializable;

/*
主控端
 */
public class Account  implements Serializable{
    private int accountID;
    private String name;
    //主控端引用被控端的对象
    private Address address;
    public Account() {
    }
    public int getAccountID() {
        return accountID;
    }
    public void setAccountID(int id) {
        this.accountID = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
}
Address.java
package entity;

/**
 * Created by lenovo on 2016/3/30.
 */
/*
被控端
 */
public class Address {
    private int addressID;
    private String address;

    public Address() {
    }

    public int getAddressID() {
        return addressID;
    }

    public void setAddressID(int id) {
        this.addressID = id;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
Account.hbm.xml
<!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="entity.Account" table="account">
        <id name="accountID" column="id" >
            <generator class="native"/>
        </id>
        <property name="name" column="name" />
        <many-to-one name="address" column="addressID" unique="true"/>
    </class>

</hibernate-mapping>

Address.hbm.xml

<!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="entity.Address" table="address">
        <id name="addressID" column="id" >
            <generator class="native"/>
        </id>
        <property name="address" column="address" />

    </class>

</hibernate-mapping>


 public void add() {
        Session session=getSession();
        Transaction transaction=session.beginTransaction();
        try{
            Address address=new Address();
            address.setAddress("beijing");
            session.save(address);

            Account account=new Account();
            account.setName("qiqi");
            account.setAddress(address);
            session.save(account);
            /*
            无法保存 一对一对应关系中,多个对象不能对应同一个地址id
            Address address=new Address();
            address.setAddress("beijing");
            session.save(address);

            Account account=new Account();
            account.setName("qiqi");
            account.setAddress(address);
            session.save(account)
            ;
            Account account=new Account();
            account.setName("hehe");
            account.setAddress(address);
            session.save(account);
             */
            transaction.commit();
        }catch (Exception e){
            transaction.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }

基于外键的双向向一对一关联映射


需要在一段添加<one-to-one>标签,用perperty-ref来指定反向属性引用

Account2.hbm.xml

<!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="entity2.Account2" table="account2">
        <id name="accountID" column="id" >
            <generator class="native"/>
        </id>
        <property name="name" column="name" />
        <many-to-one name="address" column="addressID" unique="true"/>
    </class>

</hibernate-mapping>


Address.hbm.xml
<!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="entity2.Address2" table="address2">
        <id name="addressID" column="id" >
            <generator class="native"/>
        </id>
        <property name="address" column="address" />
        <!--perperty-fer属性指定反向属性引用-->
        <one-to-one name="account" property-ref="address"/>

    </class>

</hibernate-mapping>


基于主键的单向一对一关联映射

Citizen.hbm.xml

<!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="entity3.Citizen" table="citizen">
        <id name="id" column="id" >
            <generator class="native"/>
        </id>
        <property name="name" column="name" />
        <!--constrained 通知当前主键,主键的值采用另一个表中的主键的值
        ,当前逐渐对于有关系的另一个表来说为外键-->

    </class>

</hibernate-mapping>

IDCard.hbm.xml

<!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="entity3.IDCard" table="idcard">
        <id name="id" column="id" >
            <generator class="foreign">
                <param name="property">citizen</param>
            </generator>
        </id>
        <property name="name" column="name" />
        <!--constrained 通知当前主键,主键的值采用另一个表中的主键的值
        ,当前逐渐对于有关系的另一个表来说为外键-->
        <one-to-one name="citizen" constrained="true"/>
    </class>

</hibernate-mapping>

基于主键的双向一对一关联映射

IDCard.hbm.xml

<!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="entity3<pre name="code" class="html"><!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="entity4.Citizen" table="citizen">
        <id name="id" column="id" >
            <generator class="native"/>
        </id>
        <property name="name" column="name" />
        <one-to-one name="idCard"/>
    </class>
</hibernate-mapping>

Citizen.hbm.xml

<!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="entity4.Citizen" table="citizen">
        <id name="id" column="id" >
            <generator class="native"/>
        </id>
        <property name="name" column="name" />
        <one-to-one name="idCard"/>
    </class>
</hibernate-mapping>

单向一对多映射

在对象关系映射文件中使用<one-to-many>标签映射

单向多对一映射