论坛首页 综合技术论坛

Hibernate ORM Many to Many

浏览 1232 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-07-20  

  探讨下Hibernate中对象间多对多的关联关系。以同学借阅图书为例,一个同学可以借阅多本图书,而一本图书可以被多个同学借阅。

  建议对于多对多的关系处理借助中间类,从数据库实现上就是说借助第三张表。当然具体需求具体分析,你也可以采用其他的策略。不过这里重点介绍这种方式。来看看实现吧。

  1.Student.java

 

package com.template.model;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ZhongGang
 * Date: 11-7-20
 * Time: 下午9:31
 * To change this template use File | Settings | File Templates.
 */
public class Student {
    private Integer id;
    private String name;

    private List<Borrowed> borroweds;

    public Student() {
    }
}

 

 

  2.Student.hbm.xml

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-access="field">

    <class name="com.template.model.Student" table="student">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>

        <bag name="borroweds" table="borrowed">
            <key column="studentid"/>
            <one-to-many class="com.template.model.Borrowed"/>
        </bag>
    </class>

</hibernate-mapping>

 

  3.Book.java

 

package com.template.model;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ZhongGang
 * Date: 11-7-20
 * Time: 下午9:31
 * To change this template use File | Settings | File Templates.
 */
public class Book {
    private Integer id;
    private String name;

    private List<Borrowed> borroweds;

    public Book() {
    }
}

 

  4.Book.hbm.xml

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-access="field">

    <class name="com.template.model.Book" table="book">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>

        <bag name="borroweds" table="borrowed">
            <key column="bookid"/>
            <one-to-many class="com.template.model.Borrowed"/>
        </bag>
    </class>

</hibernate-mapping>

 

  5.Borrowed.java

 

package com.template.model;

/**
 * Created by IntelliJ IDEA.
 * User: ZhongGang
 * Date: 11-7-20
 * Time: 下午9:31
 * To change this template use File | Settings | File Templates.
 */
public class Borrowed {
    private Integer id;
    private Student student;
    private Book book;

    public Borrowed() {
    }
}

 

  6.Borrowed.hbm.xml

 

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-access="field">

    <class name="com.template.model.Borrowed" table="borrowed">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>

        <many-to-one name="book" class="com.template.model.Book" column="bookid"/>
        <many-to-one name="student" class="com.template.model.Student" column="studentid"/>
    </class>

</hibernate-mapping>

 

 

  Borrwed就是辅助中间类了,用于维护学生和书籍之间的多对多关系。借助中间类通过两个一对多的关联关系来实现多对多的关系,这样的好处是有助于以后的扩展。如果一个多对多的关联上又有多个属性的话,就有必要将关系抽象出来作为一个对象来处理了,使得程序结构清晰,也有助于编程和理解。

论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics