Hibernate中的一对多和多对一

many-to-one与one-to-many映射文件

有两个实体——城市与国家,关系为n:1,欲实现关联关系的双向关联

城市(City.java)

package com.hibernate.beans;
/**
 *  城市实体
 * @author 浪丶荡
 *
 */
public class City {

    //域属性
    private Integer cityId;
    private String cityName;
    //关联属性
    private Country country;
    public City() {
        super();
    }
    public City(String cityName) {
        super();
        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;
    }
    public Country getCountry() {
        return country;
    }
    public void setCountry(Country country) {
        this.country = country;
    }
    @Override
    public String toString() {
        return "City [cityId=" + cityId + ", cityName=" + cityName + "]";
    }


}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

国家(Country.java)

package com.hibernate.beans;

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

/**
 *  国家实体
 * @author 浪丶荡
 *
 */
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;
    }
    public Set<City> getCitys() {
        return citys;
    }
    public void setCitys(Set<City> citys) {
        this.citys = citys;
    }
    @Override
    public String toString() {
        return "Country [countryId=" + countryId + ", countryName="
                + countryName + ", citys=" + citys + "]";
    }

}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

映射文件(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"/>
        </id>
        <property name="countryName" column="t_countryName"></property>
        <set name="citys" cascade="save-update">
            <!-- 当前类的ID在被关联表中的名字(这个名字将出现在City中)
                 也就是t_city表中的外键
             -->
            <key column="t_countryId"></key>
            <one-to-many class="City"/>
        </set>
     </class>
 </hibernate-mapping>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

映射文件(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"/>
        <many-to-one name="country" cascade="save-update" class="Country">
            <!--  column属性指定的是按照哪一个外键加载该持久化类 -->
            <column name="t_countryId"></column>
        </many-to-one>
    </class>
 </hibernate-mapping>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
            </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值