Hibernate ORM - 一对多双向关联关系

 

  对于一对多的关联关系,通常的做法就是在多方映射的数据表中增加一个外键字段,用以保持一方的主键标识符,维持一对多的关联关系。

 

  一。Husband

 

package com.orm.model;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/18/11
 * Time: 3:23 PM
 */
public class Husband extends DomainObject {
    private String name;

    private List<Wife> wifes;

    public Husband(String name, List<Wife> wifes) {
        this.name = name;
        this.wifes = wifes;
    }
}

 

<?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 default-access="field">

    <class name="com.orm.model.Husband" table="husband">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>

        <bag name="wifes" cascade="all">
            <key column="husbandid"/>
            <one-to-many class="com.orm.model.Wife"/>
        </bag>
    </class>

</hibernate-mapping>
 

  二。Wife

 

package com.orm.model;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/18/11
 * Time: 3:23 PM
 */
public class Wife extends DomainObject {
    private String name;

    private Husband husband;

    public Wife(String name) {
        this.name = name;
    }
}

 

<?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 default-access="field">

    <class name="com.orm.model.Wife" table="wife">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>

        <many-to-one name="husband" column="husbandid" class="com.orm.model.Husband"/>
    </class>

</hibernate-mapping>

 

  三。测试代码

 

package com.orm;

import com.orm.model.Husband;
import com.orm.model.Wife;
import com.orm.service.CoupleService;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/18/11
 * Time: 3:40 PM
 */
public class HibernateOneToManyTest extends TestCase {
    private CoupleService coupleService;

    @Override
    public void setUp() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:testDataSource.xml");
        coupleService = (CoupleService) context.getBean("coupleService");
    }

    public void testOneToOne() throws Exception {
        Wife wife1 = new Wife("wife1");
        Wife wife2 = new Wife("wife2");
        Wife wife3 = new Wife("wife3");

        List<Wife> wifes = new ArrayList<Wife>();
        wifes.add(wife1);
        wifes.add(wife2);
        wifes.add(wife3);

        Husband husband = new Husband("husband", wifes);
        coupleService.saveOrUpdate(husband);
    }
}

 

  测试结果截图

 

  sql

 

  table

 

  这是一个典型的一对多的双向关联关系,如果你需要的只是一对多的单向关联关系,删除任何一方对另一方的引用的同时删除相应配置文件中的many-to-one或者相应的集合元素即可。最后附上源码以供参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值