`
Wind_ZhongGang
  • 浏览: 259730 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Hibernate one-to-many 双方双向连接表关联

阅读更多


  Hibernate中持久化实体间一对多关联,具体关联关系为多方,单向,连接表关联。

 

  一。Husband

 

package com.dream.model.couple;

import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/17/11
 * Time: 1:06 PM
 */
public class Husband extends DomainObject {
    private String name;
    private Set<Wife> wifes;

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

    public Husband() {
    }

    public String name() {
        return name;
    }

    public Set<Wife> wifes() {
        return this.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.dream.model.couple.Husband" table="husband" dynamic-insert="true" dynamic-update="true">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <version name="version" column="version" type="java.lang.Integer"/>
        <property name="name" column="name" type="java.lang.String"/>

        <set name="wifes" table="couple" cascade="all" lazy="false">
            <key column="husbandid"/>
            <many-to-many column="wifeid" class="com.dream.model.couple.Wife" unique="true"/>
        </set>
    </class>

</hibernate-mapping>

 

  二。Wife

 

package com.dream.model.couple;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/17/11
 * Time: 1:06 PM
 */
public class Wife extends DomainObject {
    private String name;
    private Husband husband;

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

    public Wife() {
    }
}

 

<?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.dream.model.couple.Wife" table="wife" dynamic-insert="true" dynamic-update="true">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <version name="version" column="version" type="java.lang.Integer"/>
        <property name="name" column="name" type="java.lang.String"/>

        <join table="couple" optional="true">
            <key column="wifeid"/>
            <many-to-one name="husband"  column="husbandid" class="com.dream.model.couple.Husband"/>
        </join>
    </class>

</hibernate-mapping>

 

   三。Test

 

package com.dream.couple;

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

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

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 10/17/11
 * Time: 1:31 PM
 */
public class HibernateOneToManyJoinTest 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 testOneToManyDoubleDirectionJoin() throws Exception {
        Wife wife1 = new Wife("wife1");
        Wife wife2 = new Wife("wife2");
        Wife wife3 = new Wife("wife3");

        Set<Wife> wifes = new HashSet<Wife>();
        wifes.add(wife1);
        wifes.add(wife2);
        wifes.add(wife3);

        Husband husband = new Husband("husband", wifes);

        coupleService.saveOrUpdate(husband);
    }
}

 

  跑完测试,发出的sql语句

 

  result1

 

  来看看跑完测试后,数据库中相关的数据表及数据:

 

  result2

 

  result3


 

  • 大小: 19.9 KB
  • 大小: 17.8 KB
  • 大小: 12 KB
分享到:
评论

相关推荐

    HibernateAPI中文版.chm

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    hibernate3.2中文文档(chm格式)

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate+中文文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate中文详细学习文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate_Annotation关联映射

    和其它许多批注一样,在多对多关联中很多值是自动生成,党双向多对多关联中没有定义任何物理映射时,Hibernate根据以下规则生成相应的值,关联表名:主表表名+下划线+从表表名,关联到主表的外键名:主表名+下划线+...

    hibernate学习笔记

    hibernate多对一关联映射(Hibernate_Many2One) 7 hibernate一对一主键关联映射(单向关联Person----&gt;IdCard) 8 hibernate一对一主键关联映射(双向关联Person&lt;----&gt;IdCard) 9 hibernate一对一唯一外键关联映射...

    Hibernate 中文 html 帮助文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...

    hibernate 体系结构与配置 参考文档(html)

    一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...

    最全Hibernate 参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...

    Hibernate教程

    22.2. 双向的一对多关系(Bidirectional one-to-many) 22.3. 级联生命周期(Cascading lifecycle) 22.4. 级联与未保存值(Cascades and unsaved-value) 22.5. 结论 23. 示例:Weblog 应用程序 23.1. 持久化类 ...

    Hibernate3的帮助文档

    7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    NHibernate中文文档

    一对多关联(One-To-Many Associations) 14 延迟初始化(延迟加载)(Lazy Initialization) 14 集合排序(Sorted Collections) 14 使用 &lt;idbag&gt; 14 双向关联(Bidirectional Associations) 14 三重关联(Ternary ...

    hibernate 框架详解

    一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    hibernate3.04中文文档.chm

    22.2. 双向的一对多关系(Bidirectional one-to-many) 22.3. 级联生命周期(Cascading lifecycle) 22.4. 级联与未保存值(Cascades and unsaved-value) 22.5. 结论 23. 示例:Weblog 应用程序 23.1. 持久化类 ...

    Hibernate3+中文参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...

    Hibernate参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...

    hibernate_reference中文文档.pdf

    1.2.5. 双向关联 ....................................................... 18 1.2.6. 使双向连起来 ................................................... 19 1.3. 第三部分 - EventManager web 应用程序 ...........

    jdbc基础和参考

    many-to-one:标签中对于cascade的取值delete,delete-orphan,all-delete-orphan(只用unique属性值不为true不能出现)慎用 cascade:级联属性 none:不做任何级联操作 save-update:对当前对象执行save,update, ...

    NHibernate中文帮组文档(2008.11月更新)

    18.2. 双向的一对多关系(Bidirectional one-to-many) 18.3. 级联生命周期(Cascading lifecycle) 18.4. 使用级联更新 18.5. 结论 19. 示例:Weblog 应用程序 19.1. 持久化类 19.2. NHibernate 映射 19.3. ...

Global site tag (gtag.js) - Google Analytics