集合映射
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的打赏,我们会更加努力! 如果您想成为作者,请点我