HIbernate 的各种关系配置
- - ITeye博客1.主键关联one-to-one. 释义:两个表使用相同的主键列值. 例如: 身份证(IDCard):id , cardno. 中国公民(Person):id , personname. . .
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.sh.entity.Person" table="person" catalog="erp"> <id name="id" type="java.lang.Integer"> <column name="ID"/> <generator class="native"/> </id> <property name="personName" type="java.lang.String"> <column name="PERSON_NAME" length="20"/> </property> <one-to-one name="idCard" class="com.sh.entity.IdCard" cascade="all" outer-join="true"/> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.sh.entity.Person" table="person" catalog="erp"> <id name="id" type="java.lang.Integer"> <column name="ID"/> <!-- 设置id_card 的主键是一个外键 --> <generator class="foreign"> <!-- 设置主键的参考列是下面 定义的person --> <param name="property">person</param> </generator> </id> <property name="cardNo" type="java.lang.String"> <column name="CARD_NO" length="25"/> </property> <!-- 限制id_card的主键与person的主键相同 ,在属性上通过 constrained=true (约束条件) --> <one-to-one name="person" class="com.sh.entity.Person" constrained="true"/> </class> </hibernate-mapping>
属性值 | 描述 |
all | 所以情况下都进行关联操作 |
none | 所有情况下不进行关联操作 |
save-update | 执行 save-upate 操作是关联 |
delete | 执行delete 关联操作时进行关联 |
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 唯一外键关联 --> <hibernate-mapping> <class name="com.sh.entity.Person" table="person" catalog="erp"> <id name="id" type="java.lang.Integer"> <column name="ID"/> <generator class="native"/> </id> <!-- 使用 唯一外键 unique="true" 将 多对1 变成 1 对1 --> <many-to-one name="person" class="com.sh.entity.Person" unique="true" fetch="select"> <column name="PERSON_ID"/> </many-to-one> <property name="cardNo" type="java.lang.String"> <column name="CARD_NO" length="25"/> </property> <!-- 限制id_card的主键与person的主键相同 ,在属性上通过 constrained=true (约束条件) --> <one-to-one name="person" class="com.sh.entity.Person" constrained="true"/> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 唯一外键关联 --> <hibernate-mapping> <class name="com.sh.entity.Person" table="person" catalog="erp"> <id name="id" type="java.lang.Integer"> <column name="ID"/> <generator class="native"/> </id> <property name="personName" type="java.lang.String"> <column name="PERSON_NAME" length="20"/> </property> <!--注意这里和 主键关联的区别--> <one-to-one name="IdCard" class="com.sh.entity.IdCard" property-ref="person"/> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 单向一对多关联 --> <hibernate-mapping> <class name="com.sh.entity.Classes" table="classes" catalog="erp"> <id name="classesid" type="java.lang.Integer"> <column name="CLASSES_ID"/> <generator class="native"/> </id> <property name="classesName" type="java.lang.String"> <column name="CLASSES_NAME" length="25"/> </property> <property name="teacher" type="java.lang.String"> <column name="TEACHER" length="25"/> </property> <property name="createDate" type="java.lang.String"> <column name="CREATE_DATE" length="25"/> </property> <!-- 设置一对多的关联 inverse="true" 表示 学生为主控方 添加的时候要先添加 学生 然后添加 班级--> <set name="students" inverse="true"> <key> <column name="CLASSES_ID"/> </key> <one-to-many class="com.sh.entity.Student"/> </set> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 单向一对多关联 --> <hibernate-mapping> <class name="com.sh.entity.Studnt" table="student" catalog="erp"> <id name="stuid" type="java.lang.Integer"> <column name="ID"/> <generator class="native"/> </id> <property name="stuName" type="java.lang.String"> <column name="STUNAME" length="25"/> </property> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 单向 多对一 关联 --> <hibernate-mapping> <class name="com.sh.entity.Studnt" table="student" catalog="erp"> <id name="stuid" type="java.lang.Integer"> <column name="ID"/> <generator class="native"/> </id> <!-- 设置单向 多对一关联 --> <many-to-one name="classes" class="com.sh.Classes" fetch="select"> <column name="CLASSES_ID"/> </many-to-one> <property name="stuName" type="java.lang.String"> <column name="STUNAME" length="25"/> </property> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 单向多对一关联 --> <hibernate-mapping> <class name="com.sh.entity.Classes" table="classes" catalog="erp"> <id name="classesid" type="java.lang.Integer"> <column name="CLASSES_ID"/> <generator class="native"/> </id> <property name="classesName" type="java.lang.String"> <column name="CLASSES_NAME" length="25"/> </property> <property name="teacher" type="java.lang.String"> <column name="TEACHER" length="25"/> </property> <property name="createDate" type="java.lang.String"> <column name="CREATE_DATE" length="25"/> </property> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 双向向一对多关联 --> <hibernate-mapping> <class name="com.sh.entity.Classes" table="classes" catalog="erp"> <id name="classesid" type="java.lang.Integer"> <column name="CLASSES_ID"/> <generator class="native"/> </id> <property name="classesName" type="java.lang.String"> <column name="CLASSES_NAME" length="25"/> </property> <property name="teacher" type="java.lang.String"> <column name="TEACHER" length="25"/> </property> <property name="createDate" type="java.lang.String"> <column name="CREATE_DATE" length="25"/> </property> <set name="students" inverse="true" cascade="all"> <key> <column name="CLASSES_ID"/> </key> <one-to-many class="com.sh.entity.Student"/> </set> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 双向 多对一 关联 --> <hibernate-mapping> <class name="com.sh.entity.Studnt" table="student" catalog="erp"> <id name="stuid" type="java.lang.Integer"> <column name="ID"/> <generator class="native"/> </id> <!-- 设置单向 多对一关联 --> <many-to-one name="classes" class="com.sh.Classes" fetch="select"> <column name="CLASSES_ID"/> </many-to-one> <property name="stuName" type="java.lang.String"> <column name="STUNAME" length="25"/> </property> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 单向多对多关联 --> <hibernate-mapping> <class name="com.sh.entity.Employee" table="employee" catalog="erp"> <id name="empId" type="java.lang.Integer"> <column name="EMP_ID"/> <generator class="native"/> </id> <property name="empName" type="java.lang.String"> <column name="EMP_NAME" length="25"/> </property> <set name="addresses"> <!-- KEY:和当前 类的关联字段 --> <key> <column name="EMP_ID" not-null="true"/> </key> <many-to-many class="com.sh.entity.Address"/> </set> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 单向多对多关联 --> <hibernate-mapping> <class name="com.sh.entity.Address" table="address" catalog="erp"> <id name="addId" type="java.lang.Integer"> <column name="ADD_ID"/> <generator class="native"/> </id> <property name="addName" type="java.lang.String"> <column name="ADD_NAME" length="25"/> </property> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 双向多对多关联 --> <hibernate-mapping> <class name="com.sh.entity.Employee" table="employee" catalog="erp"> <id name="empId" type="java.lang.Integer"> <column name="EMP_ID"/> <generator class="native"/> </id> <property name="empName" type="java.lang.String"> <column name="EMP_NAME" length="25"/> </property> <set name="addresses" table="emp_addess" cascade="save-update"> <!-- KEY:和当前 类的关联字段 --> <key> <column name="EMP_ID" not-null="true"/> </key> <many-to-many class="com.sh.entity.Address" column="ADD_ID"/> </set> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 双向多对多关联 --> <hibernate-mapping> <class name="com.sh.entity.Address" table="address" catalog="erp"> <id name="addId" type="java.lang.Integer"> <column name="ADD_ID"/> <generator class="native"/> </id> <property name="addName" type="java.lang.String"> <column name="ADD_NAME" length="25"/> </property> <!-- 双向的 两个 类 都需 通过 集合属性 --> <set name="emps" table="emp_address" cascade="save-update"> <key> <column name="ADD_ID"/> </key> <many-to-many class="com.sh.entity.Employee" column="EMP_ID"/> </set> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 分解双向多对多关联 --> <hibernate-mapping> <class name="com.sh.entity.Employee" table="employee" catalog="erp"> <id name="empId" type="java.lang.Integer"> <column name="EMP_ID"/> <generator class="native"/> </id> <property name="empName" type="java.lang.String"> <column name="EMP_NAME" length="25"/> </property> <!-- 都是主控方 ---> <set name="addresses" table="emp_addess" inverse="true" cascade="save-update"> <!-- KEY:和当前 类的关联字段 --> <key> <column name="EMP_ID" not-null="true"/> </key> <!-- 注意 这里采用的是 一对多 --> <one-to-many class="com.sh.entity.Address"/> </set> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 分解双向多对多关联 --> <hibernate-mapping> <class name="com.sh.entity.Address" table="address" catalog="erp"> <id name="addId" type="java.lang.Integer"> <column name="ADD_ID"/> <generator class="native"/> </id> <property name="addName" type="java.lang.String"> <column name="ADD_NAME" length="25"/> </property> <!-- 双向的 两个 类 都需 通过 集合属性 都是主控方 --> <set name="emps" table="emp_address" inverse="true" cascade="all"> <key> <column name="ADD_ID"/> </key> <!-- 注意这里 采用的 一对多 的关系 --> <one-to-many class="com.sh.entity.Employee"/> </set> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 分解双向多对多关联 --> <hibernate-mapping> <class name="com.sh.entity.EmpAddress" table="emp_address" catalog="erp"> <id name="eaId" type="java.lang.Integer"> <column name="EA_ID"/> <generator class="native"/> </id> <many-to-one name="employee" class="com.sh.entity.Employee" fetch="select"> <column name="EMP_ID"/> </many-to-one> <many-to-one name="address" class="com.sh.entity.Address" fetch="select"> <column name="ADD_ID"/> </many-to-one> </class> </hibernate-mapping>