hibernate.cfg.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD
2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!-- Generated file - Do not edit! -->
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<!-- properties -->
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>
<property name="connection.username">test</property>
<property name="connection.password">123456</property>
<property
name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property
name="connection.url">jdbc:microsoft:sqlserver://192.168.1.101:1433;DatabaseName=rbac;Se
lectMethod=cursor</property>
<!-- mapping files -->
<mapping resource="org/rbac/bean/Role.hbm.xml"/>
<mapping resource="org/rbac/bean/Vehicle.hbm.xml"/>
<mapping resource="org/rbac/bean/Permission.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Role.java
/*
* 创建日期 2005-10-9
* 黄安邦
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package org.rbac.bean;
import java.util.HashSet;
import java.util.Set;
/**
* @hibernate.class table="rbac_role"
*/
public class Role {
private String id;
private String name;
private Set permission = new HashSet();
/**
* @hibernate.id generator-class="uuid.hex"
* @return 返回 id。
*/
public String getId() {
return id;
}
/**
* @param id 要设置的 id。
*/
public void setId(String id) {
this.id = id;
}
/**
* @hibernate.property
*/
public String getName() {
return name;
}
/**
* @param name 要设置的 name。
*/
public void setName(String name) {
this.name = name;
}
/**
* @hibernate.set role="permission" table="rbac_role_permission"
* @hibernate.collection-key column="role_id"
* @hibernate.collection-many-to-many class="org.rbac.bean.Permission"
column="permission_id"
* @return java.util.Set
*/
public Set getPermission() {
return permission;
}
/**
* @param permission 要设置的 permission。
*/
public void setPermission(Set permission) {
this.permission = permission;
}
}
Permission.java
/*
* 创建日期 2005-10-9
* 黄安邦
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package org.rbac.bean;
import java.util.HashSet;
import java.util.Set;
/**
* @hibernate.class table="rbac_permissions"
*/
public class Permission {
private String id;
private String name;
private String permission;
private Set role = new HashSet();
/**
* @hibernate.id generator-class="uuid.hex"
* @return 返回 id。
*/
public String getId() {
return id;
}
/**
* @param id 要设置的 id。
*/
public void setId(String id) {
this.id = id;
}
/**
* @hibernate.property
*/
public String getName() {
return name;
}
/**
* @param name 要设置的 name。
*/
public void setName(String name) {
this.name = name;
}
/**
* @hibernate.property
*/
public String getPermission() {
return permission;
}
/**
* @param permission 要设置的 permission。
*/
public void setPermission(String permission) {
this.permission = permission;
}
/**
* @hibernate.set role="role" table="rbac_role_permission"
* @hibernate.collection-key column="permission_id"
* @hibernate.collection-many-to-many class="org.rbac.bean.Role" column="role_id"
* @return java.util.Set
*/
public Set getRole() {
return role;
}
/**
* @param role 要设置的 role。
*/
public void setRole(Set role) {
this.role = role;
}
}
rbac.sql
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].
[rbac_permissions]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[rbac_permissions]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[rbac_role]') and
OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[rbac_role]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].
[rbac_role_permission]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[rbac_role_permission]
GO
CREATE TABLE [dbo].[rbac_permissions] (
[id] [varchar] (32) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[permission] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[rbac_role] (
[id] [varchar] (32) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[rbac_role_permission] (
[role_id] [varchar] (32) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[permission_id] [varchar] (32) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
Main.java
public class Main {
public static void main(String[] args) throws HibernateException {
SessionFactory sessionFactory = new Configuration().configure
().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
/*
* 添加角色,权限,并且分配
*
Role role1 = new Role();
role1.setName("角色1");
Role role2 = new Role();
role2.setName("角色2");
Role role3 = new Role();
role3.setName("角色3");
Permission p1 = new Permission();
p1.setName("添加");
p1.setPermission("add");
Permission p2 = new Permission();
p2.setName("修改");
p2.setPermission("update");
Permission p3 = new Permission();
p3.setName("删除");
p3.setPermission("delete");
Permission p4 = new Permission();
p4.setName("查看");
p4.setPermission("view");
role1.getPermission().add(p1);
role1.getPermission().add(p2);
role1.getPermission().add(p3);
role1.getPermission().add(p4);
role2.getPermission().add(p1);
role2.getPermission().add(p2);
role2.getPermission().add(p3);
role2.getPermission().add(p4);
role3.getPermission().add(p1);
role3.getPermission().add(p2);
role3.getPermission().add(p3);
role3.getPermission().add(p4);
session.save(role1);
session.save(role2);
session.save(role3);
session.save(p1);
session.save(p2);
session.save(p3);
session.save(p4);
*/
/*
* 添加角色并且分配权限
*
Role role = new Role();
role.setName("角色4");
Permission p1 = (Permission)session.load(Permission.class,
"4028818c06d8637e0106d86380a30004");
Permission p2 = (Permission)session.load(Permission.class,
"4028818c06d8637e0106d86380a30005");
Permission p3 = (Permission)session.load(Permission.class,
"4028818c06d8637e0106d86380a30006");
Permission p4 = (Permission)session.load(Permission.class,
"4028818c06d8637e0106d86380a30007");
role.getPermission().add(p1);
role.getPermission().add(p2);
role.getPermission().add(p3);
role.getPermission().add(p4);
session.save(role);
*/
/*
* 删除角色
*
Role role = (Role)session.load(Role.class, "4028818c06d865f90106d865fcde0001");
session.delete(role);
*/
/*
* 删除权限
*
Permission per = (Permission)session.load(Permission.class,
"4028818c06d8637e0106d86380a30004");
session.delete(per);
*/
/*
* 添加权限
*
Permission per = new Permission();
per.setName("添加");
per.setPermission("add");
session.save(per);
*/
/*
* 打印角色具有的权限
*
Role role = (Role)session.load(Role.class, "4028818c06d8637e0106d86380a30001");
Set list = role.getPermission();
Iterator iter = list.iterator();
while(iter.hasNext())
{
Permission p = (Permission)iter.next();
System.out.println("---------------------------------");
System.out.println(p.getName());
System.out.println("---------------------------------");
}
*/
/*
* 打印权限对应的角色
*
Permission per = (Permission)session.load(Permission.class,
"4028818c06d8637e0106d86380a30005");
Set list = per.getRole();
Iterator iter = list.iterator();
while(iter.hasNext())
{
Role r = (Role)iter.next();
System.out.println("---------------------------------");
System.out.println(r.getName());
System.out.println("---------------------------------");
}
*/
tx.commit();
session.close();
sessionFactory.close();
}
}
心得:
看了很多关于hibernate的many-to-many的文章, 今天自己实际操作了一把, 生成xml文件是用
的ant, 详细代码就不贴出来了.
发现在2端都不用inverse和cascade比较好(当然这种情况用于2端都想要当主控方), 唯一在
Main.java中的"添加角色,权限,并且分配"只需要一方add另外一方即可. 其他的不必多说,一看就明
白.