hibernate one2many (双向关联)
Posted 错莫难瞒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate one2many (双向关联)相关的知识,希望对你有一定的参考价值。
跟单向关联仅有几处不同:
1从多的一方加入新的属性
package com.cnblogs.hibernate_first; public class Student { private int id; private String name; private Classes classes; public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2修改xml文件.Classes.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.cnblogs.hibernate_first"> <class name="Classes" table="t_classes"> <id name="id" column="t_id"> <generator class="native"></generator> </id> <property name="name" column="t_name" length="30"/> <!-- 将set student加入inverse 属性设为true ,则表示该字段放弃对多的一方的管理,表示从一的一方添加失效 --> <set name="students" inverse="true"> <key column="classesid"></key> <one-to-many class="com.cnblogs.hibernate_first.Student"/> </set> </class> </hibernate-mapping>
3,修改Student.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.cnblogs.hibernate_first"> <class name="Student" table="t_student"> <id name="id" column="t_id"> <generator class="native"></generator> </id> <property name="name" column="t_name" length="30"/> <many-to-one name="classes" column="classesid"></many-to-one> </class> </hibernate-mapping>
以上是关于hibernate one2many (双向关联)的主要内容,如果未能解决你的问题,请参考以下文章