内容简介:集合映射public class User {private int id;
集合映射
public class User {
private int id;
private String password;
private String name;
private Set<String> addressSet;
//setter+getter提供set get方法
}
public class User {
private int id;
private String password;
private String name;
private Set<String> addressSet;
//setter+getter提供set get方法
}
<hibernate-mapping package=”collection”>
<class name=”User” table=”t_user”>
<id name=”id” column=”id”>
<generator class=”native”></generator>
</id>
<property name=”password” column=”password”></property>
<property name=”name” column=”name”></property>
<set name=”addressSet” table=”t_address”>
<key column=”user_id”></key>
<element column=”address” type=”string”></element>
</set>
</class>
</hibernate-mapping>
一对多
创建两个对象,在一的一方维护多的一方的集合
注意:两个对象必须都有映射文件
在一的一方配置
<hibernate-mapping package=”one2many”>
<class name=”User” table=”t_user”>
<id name=”id” column=”id”>
<generator class=”native”></generator>
</id>
<property name=”password” column=”password”></property>
<property name=”name” column=”name”></property>
<set name=”address” table=”t_address”>
<key column=”user_id”></key>
<one-to-many class=”Address”/>
</set>
</class>
</hibernate-mapping>
多对一
创建两个对象,在多的一方维护多的一方的对象
注意:两个对象必须都有映射文件
在多的一方配置
<hibernate-mapping package=”one2many2one”>
<class name=”Address” table=”t_address”>
<id name=”id” column=”id”>
<generator class=”native”></generator>
</id>
<property name=”address” column=”address”></property>
<property name=”city” column=”city”></property>
<property name=”country” column=”country”></property>
<many-to-one name=”user” column=”user_id” class=”User”></many-to-one>
</class>
双向配置
即一对多,多对一
inverse:
true不维护关系
false翻转
注意:只能在没有外键的一方放弃维护关系
放弃维护关系会出现的问题
添加时,因为一的一方不维护关系,多的一方的外键值null
删除时,因为一的一方不维护关系,多的一方还在引用一的一方的主键删不了,违法外键约束
级联操作
save-update:级联修改与保存
delete:级联删除
delete-orphan:孤儿删除,当多的一方没有任何外键应用,责备删除
all:相当与save-update ,delete
all-delete-orphan:相当与save-update ,delete delete-orphan
注意:delete不能双方同时使用,不然会造成数据丢失
多对多
多对多的表设计:双方的javaben中各自维护对方的集合
配置
<hibernate-mapping package=”a_many2many”>
<class name=”Student” table=”t_student”>
<id name=”id” column=”id”>
<generator class=”native”></generator>
</id>
<property name=”name” column=”name”></property>
<property name=”sex” column=”sex”></property>
<set name=”teachers” table=”t_student_teacher” inverse=”true”>
<key column=”s_id” ></key>
<many-to-many class=”Teacher” column=”t_id” ></many-to-many>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package=”a_many2many”>
<class name=”Teacher” table=”t_teacher”>
<id name=”id” column=”id”>
<generator class=”native”></generator>
</id>
<property name=”name” column=”name”></property>
<property name=”sex” column=”sex”></property>
<set name=”students” table=”t_student_teacher” inverse=”false” cascade=”all”>
<key column=”t_id” ></key>
<many-to-many class=”Student” column=”s_id” ></many-to-many>
</set>
</class>
</hibernate-mapping>
注意:开发时一般设置一方的放弃维护关系
一对一关系
双方的javaben中各自维护对方的对象
方式一:在任意一方维护一个外键(特殊的多对多,指定外键值唯一)
注意:这种配置只能在外键所在的一方维护关系
<hibernate-mapping package=”one2one”>
<class name=”Company” table=”t_company”>
<id name=”id” column=”id”>
<generator class=”native”></generator>
</id>
<property name=”name” column=”name”></property>
<one-to-one name=”address” class=”Address” property-ref=”company”></one-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping package=”one2one”>
<class name=”Address” table=”t_address”>
<id name=”id” column=”id”>
<generator class=”native”></generator>
</id>
<property name=”address” column=”address”></property>
<many-to-one name=”company” column=”c_id” class=”Company” unique=”true” cascade=”all”></many-to-one>
</class>
</hibernate-mapping>
方式二:在一方指定既是主键又是外键
<hibernate-mapping package=”copyone2one”>
<class name=”Company” table=”t_company”>
<id name=”id” column=”id”>
<generator class=”native”></generator>
</id>
<property name=”name” column=”name”></property>
<one-to-one name=”address” class=”Address” ></one-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping package=”copyone2one”>
<class name=”Address” table=”t_address”>
<id name=”id” column=”id”>
<generator class=”foreign”>
<param name=”property”>company</param>
</generator>
</id>
<property name=”address” column=”address”></property>
<one-to-one name=”company” class=”Company” constrained=”true”></one-to-one>
</class>
</hibernate-mapping>
转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/24277.html
微信打赏
支付宝打赏
感谢您对作者Alex的打赏,我们会更加努力! 如果您想成为作者,请点我
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
解放战争(上)(1945年8月—1948年9月)
王树增 / 人民文学出版社 / 2009-8 / 60.00
《解放战争》为王树增非虚构文学著述中规模最大的作品。武器简陋、兵力不足的军队对抗拥有现代武器装备的兵力庞大的军队,数量不多、面积有限的解放区最终扩展成为九百六十万平方公里的共和国,解放战争在短短四年时间里演绎的是人类历史上的战争传奇。国际风云,政治智慧,时事洞察,军事谋略,军队意志,作战才能,作品具有宏阔的视角和入微的体察,包含着惊心动魄的人生沉浮和变幻莫测的战场胜负,尽展中国历史上规模最大的一场......一起来看看 《解放战争(上)(1945年8月—1948年9月)》 这本书的介绍吧!