hibernate中的一对多双向关联

60 篇文章 0 订阅
27 篇文章 0 订阅

hibernate中的一对多双向关联
上一篇文章hibernate中的一对多单向关联介绍了单向关联的实现
上篇博文已经说明了双向关联是彼此把对方设为关联属性,彼此都可以加载和访问对方

一方实体(Country.java)

package com.hibernate.beans;

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

public class Country {
    //域属性
    private Integer countryId;
    private String countryName;
    //关联属性
    private Set<City> citys;
    public Country() {
        citys = new HashSet<City>();
    }
    public Country(String countryName) {
        this();
        this.countryName = countryName;
    }

    public Integer getCountryId() {
        return countryId;
    }
    public void setCountryId(Integer countryId) {
        this.countryId = countryId;
    }
    public String getCountryName() {
        return countryName;
    }
    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }
    @Override
    public String toString() {
        return "Country [countryId=" + countryId + ", countryName="
                + countryName +  "]";
    }
    public Set<City> getCitys() {
        return citys;
    }
    public void setCitys(Set<City> citys) {
        this.citys = citys;
    }


}

多方实体(City.java)

package com.hibernate.beans;

public class City {
    //域属性
    private Integer cityId;
    private String cityName;
    //关联属性
    private Country country;
    public City() {
    }

    public City(String cityName) {
        this.cityName = cityName;
    }

    public Integer getCityId() {
        return cityId;
    }

    public void setCityId(Integer cityId) {
        this.cityId = cityId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }


    @Override
    public String toString() {
        return "City [cityId=" + cityId + ", cityName=" + cityName
                +  "]";
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

}

映射文件(Country.hbm.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 <hibernate-mapping package="com.hibernate.beans">
     <class name="Country" table="t_country">
        <!-- 域属性的映射 -->
        <id name="countryId" column="t_countryId">
            <!-- 主键生成策略 -->
            <generator class="native"></generator>
        </id>
        <property name="countryName" column="t_countryName"></property>
        <!-- 关联属性的映射 -->
        <set name="citys" cascade="save-update"><!-- save-update级联保存 -->
            <!-- 一方主键对应在多方表的外键名 -->
            <key column="t_countryId"></key>
            <!-- 多方的类型 -->
            <one-to-many class="City"/>
        </set>
     </class>
 </hibernate-mapping>

映射文件(City.hbm.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 <hibernate-mapping package="com.hibernate.beans">
     <class name="City" table="t_city">
        <!-- 域属性的映射 -->
        <id name="cityId" column="t_cityId">
            <!-- 主键生成策略 -->
            <generator class="native"></generator>
        </id>
        <property name="cityName" column="t_cityName"></property>
        <!-- 关联属性的映射 -->
        <many-to-one name="country" column="t_countryId" class="Country" cascade="save-update"/>
     </class>
 </hibernate-mapping>

测试类(当1方维护关联关系时)

package com.hibernate.test;


import org.hibernate.Session;
import org.junit.Test;

import com.hibernate.beans.Capital;
import com.hibernate.beans.City;
import com.hibernate.beans.Country;
import com.hibernate.beans.Course;
import com.hibernate.beans.Student;
import com.hibernate.utils.HbnUtils;

public class TestAssociation {
    @Test
    public void test() {
        // 获取连接
        Session session = HbnUtils.getSession();
        try {
            // 开启事务
            session.beginTransaction();


            Country country = new Country("CN");
            City city = new City("武汉");
            City city1 = new City("长沙");

            country.getCitys().add(city);
            country.getCitys().add(city1);


            // 一方维护关联关系
            session.save(country);
            // 提交事务
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚
            session.getTransaction().rollback();
        }
    }
}

测试类(当多方维护关联关系时)

package com.hibernate.test;


import org.hibernate.Session;
import org.junit.Test;

import com.hibernate.beans.Capital;
import com.hibernate.beans.City;
import com.hibernate.beans.Country;
import com.hibernate.beans.Course;
import com.hibernate.beans.Student;
import com.hibernate.utils.HbnUtils;

public class TestAssociation {
    @Test
    public void test() {
        // 获取连接
        Session session = HbnUtils.getSession();
        try {
            // 开启事务
            session.beginTransaction();


            Country country = new Country("CN");
            City city = new City("武汉");
            City city1 = new City("长沙");

            city.setCountry(country);
            city1.setCountry(country);

            // 多方维护关联关系
            session.save(city);
            session.save(city1);
            // 提交事务
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚
            session.getTransaction().rollback();
        }
    }
}


如有疑问,欢迎入群讨论:511906138

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值