实战3--设计实体, 映射实体

1. Privilege.java

package cn.itcast.oa.domain;

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

public class Privilege {
	private Long id;
	private String name;  //权限名称
	private String url;
	private Set<Role> roles = new HashSet<Role>();
	private Privilege parent;  //上级权限
	private Set<Privilege> children = new HashSet<Privilege>(); //下级权限
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Set<Role> getRoles() {
		return roles;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Privilege getParent() {
		return parent;
	}
	public void setParent(Privilege parent) {
		this.parent = parent;
	}
	public Set<Privilege> getChildren() {
		return children;
	}
	public void setChildren(Set<Privilege> children) {
		this.children = children;
	}
}

2. Privilege.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 package="cn.itcast.oa.domain">
	<class name="Privilege" table="itcast_privilege">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name" />
		<property name="url" />
		<!-- roles属性, 本类与Role的 多对多 -->
		<set name="roles" table="itcast_role_privilege">
			<key column="privilegeId"></key>
			<many-to-many class="Role" column="roleId"></many-to-many>
		</set>
		<!-- parent属性, 本类与Privilege上级的多对一 -->
		<many-to-one name="parent" class="Privilege" column="parentId"></many-to-one>
		<!-- children属性, 本类与下级Privilege的一对多 -->
		<set name="children" cascade="delete" order-by="id asc">
			<key column="parentId"></key>
			<one-to-many class="Privilege"/>
		</set>
	</class>
	
</hibernate-mapping> 

3. 更新role.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 package="cn.itcast.oa.domain">
	<class name="Role" table="itcast_role">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name" />
		<property name="description" />
		<!-- users属性, 本类与User的 多对多 -->
		<set name="users" table="itcast_user_role">
			<key column="roleId"></key>
			<many-to-many class="User" column="userId"></many-to-many>
		</set>
		<!-- privileges属性, 本类与Privilege的 多对多 -->
		<set name="privileges" table="itcast_role_privilege">
			<key column="roleId"></key>
			<many-to-many class="Privilege" column="privilegeId"></many-to-many>
		</set>
	</class>
	
</hibernate-mapping>

4. 更新hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

	<session-factory>

		<!-- Database connection settings -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
			<property name="connection.url">jdbc:mysql://localhost/hibernate</property> 
			<property name="connection.username">root</property> <property name="connection.password">bjsxt</property> -->
			
		<!-- 2. other configuration -->
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		<property name="connection.pool_size">1</property>


		<!--3. mapping -->
		<mapping resource="cn/itcast/oa/domain/User.hbm.xml" />
		<mapping resource="cn/itcast/oa/domain/Role.hbm.xml" />
		<mapping resource="cn/itcast/oa/domain/Department.hbm.xml" />
		<mapping resource="cn/itcast/oa/domain/Privilege.hbm.xml" />
	</session-factory>

</hibernate-configuration>

  

posted @ 2016-05-23 13:45  wujixing909  阅读(242)  评论(0编辑  收藏  举报