首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

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

2012-07-01 
Hibernate ORM - 一对多双向组合关联关系?  对于一对多的关联关系方式还有一种比较特殊的做法就是将多方视

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 column="name" type="java.lang.String"/>        <bag name="wifes" table="couple" cascade="all">            <key column="husbandid"/>            <composite-element column="name" type="java.lang.String"/>            </composite-element>        </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 {    private String name;    private Husband husband;    public Wife() {    }    public Wife(String name) {        this.name = name;    }    public Husband getHusband() {        return husband;    }    public void setHusband(Husband husband) {        this.husband = husband;    }}

?

  三。测试代码

?

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 testOneToMany() 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);    }}

?

  测试结果截图

?

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

?

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

?

  这是一个典型的一对多双向组合关联关系,重点在于通过composite-element元素来实现,它将其一方中的多方元素视为其组成的一部分,对于多方来说没有相应的配置文件及数据表,而是由两者的连接表来表征其多方的数据及维持两者一对多的关联关系。附源码以供参考。

热点排行