标签:oct hiberna hbm.xml turn hbm return ide pen blog
跟单向关联仅有几处不同:
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>
标签:oct hiberna hbm.xml turn hbm return ide pen blog
原文地址:http://www.cnblogs.com/Juli/p/7743954.html