手记

hibernate多对多关联映射demo--以Roles和Privileges为例

开始之前说两句:
整体思路:一个角色可以有多个权限,一种权限可以被多个角色使用,对应一个角色表role,一个权限表privilege,同时会生成一个rol_pri_all角色-权限表,表示角色和权限之间的关系。使用<many-to-many>标签体现映射关系。

1.新建工程,工程目录如下

2.确定映射关系

3.建立数据库表role,privilege,rol_pri_all表结构如下



4.编写java

/************Roles.java******************/
package com.feng.dao;

import java.util.HashSet;
import java.util.Set;

public class Roles {

    private Integer id;
    private String name;

    private Set<Privileges> pris=new HashSet<Privileges>(0);

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Privileges> getPris() {
        return pris;
    }

    public void setPris(Set<Privileges> pris) {
        this.pris = pris;
    }
}
/****************Privileges.java********************/
package com.feng.dao;

import java.util.HashSet;
import java.util.Set;

public class Privileges {

    private Integer id;
    private String name;

    private Set<Roles>roles=new HashSet<Roles>(0);

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Roles> getRoles() {
        return roles;
    }

    public void setRoles(Set<Roles> roles) {
        this.roles = roles;
    }
}

5.编写java类同数据的映射文件roles.hbm.xml,privileges.hbm.xml,依次如下所示:

<!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.feng.dao.Roles" table="role">
        <id name="id">
            <column name="id" sql-type="int"></column>
            <generator class="increment" ></generator>
        </id>
        <property name="name" column="name"></property>
        <set name="pris" table="rol_pri_all" cascade="all">
            <key column="roleid"></key>
            <many-to-many class="com.feng.dao.Privileges" column="priid" ></many-to-many>
        </set>
    </class>
</hibernate-mapping>
<!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.feng.dao.Privileges" table="privilege">
        <id name="id">
            <column name="id" sql-type="int"></column>
            <generator class="increment" ></generator>
        </id>
        <property name="name" column="name"></property>
        <!-- 此处体现了many-to-many的映射 -->
        <set name="roles" table="rol_pri_all" inverse="true">
            <key column="priid"></key>
            <many-to-many class="com.feng.dao.Roles" column="roleid" ></many-to-many>
        </set>
    </class>
</hibernate-mapping>

6.创建工程同数据库的映射文件hibernate.cfg.xml

<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: 
    GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the 
    lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedemo</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="connection.characterEncoding">utf8</property>
        <property name="connection.pool_siez">10</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <!--  <mapping resource="com/feng/dao/student.hbm.xml" />-->
        <mapping resource="com/feng/dao/roles.hbm.xml" />
        <mapping resource="com/feng/dao/privileges.hbm.xml" />
    </session-factory>

</hibernate-configuration>

7.编写测试类:Test.java

package com.feng.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import com.feng.dao.Privileges;
import com.feng.dao.Roles;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 1.进行配置文件的读取
        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
        StandardServiceRegistry sr = ssrb.configure().build();
        // 2.由config得到一个会话工厂//得到sessionFactory
        SessionFactory sf = new MetadataSources(sr).buildMetadata().buildSessionFactory();
        // 3.由sessionFacatory得到session
        Session session = sf.openSession();
        /*
         * 开启事务,增删改都需要事务
         */
        Transaction tran = session.beginTransaction();

        Roles r =new Roles();
        r.setName("jingli");
        Privileges p1 =new Privileges();
        p1.setName("money");
        p1.getRoles().add(r);
        Privileges p2=new Privileges();
        p2.setName("buy");
        p2.getRoles().add(r);

        r.getPris().add(p1);
        r.getPris().add(p2);
        session.save(r);

        tran.commit(); // 一定要提交事务!!!!!!!
        // 4.记得关闭session
        session.close();
        sf.close();
    }
}

8.结果正常,这里就不详细展示了

最后的最后:在做这个程序的时候我同样也遇到了许多问题,经过多次更改,终于出来了这个结果,做完之后的心情是开心的。

学习不息,代码不止!!!
8人推荐
随时随地看视频
慕课网APP