zoukankan      html  css  js  c++  java
  • 实战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>
    

      

  • 相关阅读:
    开始我的博客
    POJ 1284:Primitive Roots(素数原根的个数)
    数据结构作业——图的存储及遍历(邻接矩阵、邻接表+DFS递归、非递归+BFS)
    NYOJ 85:有趣的数(打表,规律)
    NYOJ 12:喷水装置(二)(贪心,区间覆盖问题)
    HDU 2058:The sum problem(数学)
    HDU 1716:排列2(全排列)
    HDU 2048:神、上帝以及老天爷(错排公式,递推)
    NYOJ 6:喷水装置(一)(贪心)
    BZOJ 2002:Bounce 弹飞绵羊(分块)
  • 原文地址:https://www.cnblogs.com/wujixing/p/5519700.html
Copyright © 2011-2022 走看看