码迷,mamicode.com
首页 > Web开发 > 详细

hibernate one2many (双向关联)

时间:2017-10-27 16:36:51      阅读:195      评论:0      收藏:0      [点我收藏+]

标签: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>
View Cod

 

hibernate one2many (双向关联)

标签:oct   hiberna   hbm.xml   turn   hbm   return   ide   pen   blog   

原文地址:http://www.cnblogs.com/Juli/p/7743954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!