正在加载...

hibernate入门使用系列 5 -- xml关系映射篇(补充1)

本来对于hibernate的关系映射不打算再说了。不过前3篇的关系映射都是最原始的。在实际中,可能会有较为复杂的关系。 比方说:customers一对多于orders,同时一对一于address,这样的三表关联。

所以,再写一遍1对多和1对1同时存在的。

先看实际场景。不用上面的,用一个新的。 比方说:丈夫有1个妻子,这样是1夫1妻。但是,丈夫花心,同时有多个情妇。

例子归例子,大家不要做这样的丈夫。只是为了更加深入记忆。

先看sql:

use HibernateQuickUse;

drop table if exists Paramour;
drop table if exists Husband;
drop table if exists Wife;

create table Wife (
    wid varchar(32) primary key,
    name varchar(128) not null
);


create table Husband (
    hid varchar(32) primary key,
    name varchar(32) not null,
    wife_id varchar(32) not null,
    foreign key(wife_id) references Wife(wid)
);

create table Paramour (
    pid varchar(32)  primary key,
    name varchar(128) not null,
    husband_id varchar(32) not null,
    foreign key(husband_id) references Husband(hid)
);

然后看java文件。

Husband.java

package org.py.hib.relation.complex;

import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("serial")
public class Husband implements java.io.Serializable
{
    private String id;

    private String name;

    private Wife wife;

    private Set paramours = new HashSet();

    /** default constructor */
    public Husband()
    {
    }

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Wife getWife()
    {
        return wife;
    }

    public void setWife(Wife wife)
    {
        this.wife = wife;
    }

    public Set getParamours()
    {
        return paramours;
    }

    public void setParamours(Set paramours)
    {
        this.paramours = paramours;
    }
}

Wife.java

package org.py.hib.relation.complex;

@SuppressWarnings("serial")
public class Wife implements java.io.Serializable
{
    private String id;

    private String name;

    /** default constructor */
    public Wife()
    {
    }

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getName()
    {
        return this.name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}

Paramour.java

package org.py.hib.relation.complex;

@SuppressWarnings("serial")
public class Paramour implements java.io.Serializable
{
    private String id;

    private String name;

    private Husband husband;

    /** default constructor */
    public Paramour()
    {
    }

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public Husband getHusband()
    {
        return this.husband;
    }

    public void setHusband(Husband husband)
    {
        this.husband = husband;
    }

    public String getName()
    {
        return this.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 3.0//EN"  
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

<hibernate-mapping>  
    <class name="org.py.hib.relation.complex.Husband" table="husband">  
        <id name="id" type="java.lang.String" column="hid"  
            length="32">  
            <generator class="uuid" />  
        </id>  

        <property name="name" type="java.lang.String" column="name"  
            length="32" not-null="true" />  

        <many-to-one name="wife" class="org.py.hib.relation.complex.Wife" cascade="all" column="wife_id" />  

        <set name="paramours" cascade="all" inverse="true" >  
            <key column="husband_id" />  
            <one-to-many class="org.py.hib.relation.complex.Paramour" />  
        </set>  
    </class>  
</hibernate-mapping>

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">  

<hibernate-mapping>  
    <class name="org.py.hib.relation.complex.Wife" table="wife">  
        <id name="id" type="java.lang.String" column="wid" length="32">  
            <generator class="uuid" />  
        </id>  

        <property name="name" type="java.lang.String" column="name"  length="128" not-null="true" />  

    </class>  
</hibernate-mapping>

Paramour.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">   
<hibernate-mapping>  
    <class name="org.py.hib.relation.complex.Paramour" table="paramour">  
        <id name="id" type="java.lang.String" column="pid">  
            <generator class="uuid" />  
        </id>  

        <property name="name" type="java.lang.String" column="name" not-null="true" />  

        <many-to-one name="husband" class="org.py.hib.relation.complex.Husband" column="husband_id" />  

    </class>  

</hibernate-mapping>

这里只测试了save。其他的测试大家感兴趣的可以补充。

ComplexTest.java

package org.py.hib.relation.complex;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;

/**
 * 这个测试忽略了hibernate的异常
 */
public class ComplexTest extends TestCase
{
    private SessionFactory factory;

    @Before
    public void setUp() throws Exception
    {
        Configuration conf = new Configuration().configure();
        factory = conf.buildSessionFactory();
    }

    public void testSave()
    {
        Session session = factory.openSession();
        Transaction tran = session.beginTransaction();

        Husband hus = new Husband();
        hus.setName("husband");

        Wife wife = new Wife();
        wife.setName("wife");

        Paramour p1 = new Paramour();
        p1.setName("paramour_1");

        Paramour p2 = new Paramour();
        p2.setName("paramour_2");

        Paramour p3 = new Paramour();
        p3.setName("paramour_3");

        hus.setWife(wife);

        hus.getParamours().add(p1);
        hus.getParamours().add(p2);
        hus.getParamours().add(p3);

        p1.setHusband(hus);
        p2.setHusband(hus);
        p3.setHusband(hus);

        session.save(hus);

        tran.commit();

        Assert.assertNotNull(hus.getId());
        Assert.assertNotNull(wife.getId());

        Assert.assertNotNull(p1.getId());
        Assert.assertNotNull(p2.getId());
        Assert.assertNotNull(p3.getId());

        session.close();
    }

    @After
    public void tearDown()
    {
        factory.close();
    }
}

其实,这个例子仍然很简单,且无实用性。

附件里面是源代码。