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

5 篇文章 0 订阅
4 篇文章 0 订阅


  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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值