作者lueichun (= =)
看板java
标题[问题] Hibernate的oneToMany与ManyToOne的意义
时间Fri Jan 22 22:20:26 2021
※状况概述:
以下的程式,有City跟Country两个类别,city跟country是多对一的关系,
程式可以正常运作:
City类别:
public class City {
private int cityId;
private String cityName;
private Country country;
......
}
City的映射档:
<hibernate-mapping package="com.test.vo">
<class name="City" table="CITY">
<id name="cityId" column="CITY_ID">
<generator class="increment"/>
</id>
<property name="cityName" column="CITY_NAME" />
<many-to-one name="country" column="COUNTRY_ID" class="com.test.vo.Country"
cascade="all"/><!--country是外键属性、COUNTRY_ID是外键栏位-->
</class>
</hibernate-mapping>
Country类别:
public class Country {
int countryId;
String countryName;
String countyCreateDate;
private Set<City> cities =new HashSet<City>();
......
}
Country映射档:
<hibernate-mapping package="com.test.vo">
<class name="Country" table="COUNTRY">
<id name="countryId" column="COUNTRY_ID">
<generator class="sequence">
<param name="sequence">country_id_seq</param>
</generator>
</id>
<property name="countryName" column="COUNTRY_NAME" />
<property name="countyCreateDate" column="COUNTRY_CREATE_DATE" />
<set name="cities" table="city" cascade="all">
<key column="country_Id"/><!--country_Id是外键栏位-->
<one-to-many class="com.test.vo.City"/>
</set>
</class>
</hibernate-mapping>
※错误讯息:
以上程式可正常运行。
※补充说明:
虽然程式可以正常运行,但我不懂的是套色的部分,为何这样设定外键栏位跟外键属性
即可正常执行crud
背後的逻辑是什麽?希望可以搞懂,不然又变成只是死背语法了。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.169.108.230 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/java/M.1611325228.A.68F.html
※ lueichun:转录至看板 Database 01/30 14:59
1F:推 mureka: 我建议你改用 grails 看看 04/04 18:44