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

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>

你可能感兴趣的:(Hibernate,ssh,Java框架)