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 citys;
    public Country() {
        citys = new HashSet();
    }
    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 getCitys() {
        return citys;
    }
    public void setCitys(Set citys) {
        this.citys = citys;
    }
    @Override
    public String toString() {
        return "Country [countryId=" + countryId + ", countryName="
                + countryName + ", citys=" + citys + "]";
    }

}

映射文件(Country.hbm.xml)



 <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">
            
            <key column="t_countryId">key>
            <one-to-many class="City"/>
        set>
     class>
 hibernate-mapping>

映射文件(City.hbm.xml)



 <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 name="t_countryId">column>
        many-to-one>
    class>
 hibernate-mapping>

你可能感兴趣的:(java框架,Hibernate,SSH)