many-to-one与one-to-many映射文件关于外键的问题

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

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 + "]";
    }


}

国家(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 + "]";
    }

}

映射文件(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>

映射文件(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>
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值