说明:
1,练习到一对多、多对多、一对一映射。
2,需要管理Session,要做增删改查工作。
3,有多种查询的要求。
----------------------------
要做的事:
1,根所类图写出JavaBean。
2,写出映射文件并建表。
3,完成以下操作
------------------------------------------------------------
1,实体:
Department
Employee
UserAccount
Privilege
2,操作:
部门:
增加一个部门
更新一个部门的信息(更改名称)
把部门中所有的员工都取消关联
删除一个部门,并把所有的子部门删掉,如果部门中有员工,则不能删除。
查询最顶级部门,按id升序排列(最顶级的就是没有上级的部门)
查询某部门的所有子部门,按id升序排序
统计:员工的总数量,部站的总数量,每个部门中员工的数量。
员工:
增加一个员工
为员工分配一个用户账号(指定登录名与密码)
更新员工的信息
更改员工所属的部门
更新员工账号的密码
删除员工的账号
删除一个员工,并同时删除他的账号
根据登录名与密码查询一个员信息(登录时使用)
查询所有分配了账号的员工
把有所员工的密码都重置为"1234"(使用update语句实现)
权限:
添加若干个权限数据,用于测试(权限没有增删改,是一开始就定义好了的)
为某个员工分配一些权限(为员工关联一些权限,例如关联3个)
移除某员工中的一个权限
检测某员工是否有某权限
查询出所有含有某权限的员工
3,提示
注意属性是否与所有的数据库中的关键字冲突。
员工的生日只保存年月日。
一对一映射采用基于外键的方式。
===================================== 模板 =====================================
一对多(Set):
<set name="">
<key column="" />
<one-to-many class="" />
</set>
多对一:
<many-to-one name="" class="" column="" />
多对多(Set):
<set name="" table="">
<key column="" />
<many-to-many class="" column="" />
</set>
一对一(基于外键的有外键方):
<many-to-one name="" class="" column="" unique="true"/>
一对一(基于外键的无外键方):
<one-to-one name="" class="" property-ref=""/>
===================================== 填空 =====================================
①②③④⑤⑥⑦⑧⑨⑩
<!-- privileges属性,表示与Privilege的多对多 关系 -->
------①----------------------②---------③--------------
1,name属性:
填①
2,class属性:
填②
3,column属性:
在many-to-one中:写name属性值加id后缀。
在一对多的<key>中:写对方类的表达此关系的外键列名。
在多对多的<key>中:写自已类的名称加id后缀。
在多对多的many-to-many的column中:写对方类的名称加id后缀。
Configuration.cfg.xml
<!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 name="foo">
<!-- 方言、连接、驱动、用户名、密码 -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate_test
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 配置自动结构建表 -->
<!-- 建表策略:create、create-drop、update、validata -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 设置SQL语句显示 -->
<property name="show_sql">true</property>
<!-- c3p0连接池设定 -->
<!-- 使用c3p0连接池 配置连接池提供的供应商 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 在连接池中可用的数据库连接的最少数目 -->
<property name="c3p0.min_size">5</property>
<!-- 在连接池中所有数据库连接的最大数目 -->
<property name="c3p0.max_size">20</property>
<!-- 设定数据库连接时间,以秒为单位, 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
<property name="c3p0.timeout">120</property>
<!-- 每3000秒检查所有连接池中空闲连接 以秒为单位 -->
<property name="c3p0.idle_test_period">3000</property>
<!-- 导入配置文件 -->
<mapping resource="cn/itcast/test/entity/Department.hbm.xml" />
<mapping resource="cn/itcast/test/entity/Employee.hbm.xml" />
<mapping resource="cn/itcast/test/entity/Privilege.hbm.xml" />
<mapping resource="cn/itcast/test/entity/UserAccount.hbm.xml" />
</session-factory>
</hibernate-configuration>
Department
package cn.itcast.test.entity;
import java.util.HashSet;
import java.util.Set;
/**
* 实体:部门
*
* @author 风清杨
* @version V5.0
*/
public class Department {
/** 部门编号 */
private Long id;
/** 部门名称 */
private String name;
/** 部门说明 */
private String description;
/** 部门员工 */
private Set<Employee> employee = new HashSet<Employee>();
/** 上级部门 */
private Department parent;
/** 下级部门 */
private Set<Department> children = new HashSet<Department>();
public Department() {
super();
// TODO Auto-generated constructor stub
}
public Department(Long id, String name, String description, Set<Employee> employee, Department parent, Set<Department> children) {
super();
this.id = id;
this.name = name;
this.description = description;
this.employee = employee;
this.parent = parent;
this.children = children;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<Employee> getEmployee() {
return employee;
}
public void setEmployee(Set<Employee> employee) {
this.employee = employee;
}
public Department getParent() {
return parent;
}
public void setParent(Department parent) {
this.parent = parent;
}
public Set<Department> getchildren() {
return children;
}
public void setchildren(Set<Department> children) {
this.children = children;
}
}
Employee
package cn.itcast.test.entity;
import java.util.Date;
/**
* 实体:员工信息
*
* @author 风清杨
* @version V5.0
*/
public class Employee {
/** 员工编号 */
private Long id;
/** 员工姓名 */
private String name;
/** 员工性别 */
private boolean gender;
/** 员工生日 */
private Date birthday;
/** 员工描述*/
private String description;
/** 员工部门 */
private Department department;
/** 员工账号 */
private UserAccount userAccount;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(Long id, String name, boolean gender, Date birthday, String description, Department department, UserAccount userAccount) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.birthday = birthday;
this.description = description;
this.department = department;
this.userAccount = userAccount;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public UserAccount getUserAccount() {
return userAccount;
}
public void setUserAccount(UserAccount userAccount) {
this.userAccount = userAccount;
}
}
UserAccount
package cn.itcast.test.entity;
import java.util.HashSet;
import java.util.Set;
/**
* 实体:用户的账号
*
* @author 风清杨
* @version V5.0
*/
public class UserAccount {
/** 账号编号 */
private Long id;
/** 账号用户名 */
private String loginName;
/** 账号密码 */
private String password;
/** 账号员工 */
private Employee employee;
/** 账号权限 */
private Set<Privilege> privileges = new HashSet<Privilege>();
public UserAccount() {
super();
// TODO Auto-generated constructor stub
}
public UserAccount(Long id, String loginName, String password, Employee employee, Set<Privilege> privileges) {
super();
this.id = id;
this.loginName = loginName;
this.password = password;
this.employee = employee;
this.privileges = privileges;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public Set<Privilege> getPrivileges() {
return privileges;
}
public void setPrivileges(Set<Privilege> privileges) {
this.privileges = privileges;
}
}
Privilege
package cn.itcast.test.entity;
import java.util.HashSet;
import java.util.Set;
/**
* 实体:权限
*
* @author 风清杨
* @version V5.0
*/
public class Privilege {
/** 权限编号 */
private Long id;
/** 权限名称 */
private String action;
/** 权限员工 */
private Set<UserAccount> userAccount = new HashSet<UserAccount>();
public Privilege() {
super();
// TODO Auto-generated constructor stub
}
public Privilege(Long id, String action, Set<UserAccount> userAccount) {
super();
this.id = id;
this.action = action;
this.userAccount = userAccount;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public Set<UserAccount> getUserAccount() {
return userAccount;
}
public void setUserAccount(Set<UserAccount> userAccount) {
this.userAccount = userAccount;
}
}
Department.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 导入包名 -->
<hibernate-mapping package="cn.itcast.test.entity">
<!-- 配置类名并构建表 -->
<class name="Department" table="t_department">
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<property name="name" type="string" column="name" />
<!-- employee属性,表示与Employee的一对多的关系 -->
<set name="employee">
<key column="departmentId" />
<one-to-many class="Employee" />
</set>
<!-- parent属性,表示与Department(上级部门)的多对一关系 -->
<many-to-one name="parent" class="Department" column="parentId" />
<!-- children属性 ,表示与Department(下级部门)的一对多关系 -->
<set name="children">
<key column="parentId" />
<one-to-many class="Department" />
</set>
</class>
</hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 导入包名 -->
<hibernate-mapping package="cn.itcast.test.entity">
<!-- 配置类名并构建表 -->
<class name="Employee" table="t_employee">
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<property name="name" type="string" column="name"></property>
<property name="gender" type="boolean" column="gender"></property>
<property name="birthday" type="date" column="birthday"></property>
<property name="description" type="string" column="description"></property>
<!-- department属性,表示与Department的多对一关系 -->
<many-to-one name="department" class="Department" column="departmentId" />
<!-- userAccount属性,表示与UserAccount的一对一关系,
采用基于外键的映射方式,本方是无外键方。
-->
<one-to-one name="userAccount" class="UserAccount" property-ref="employee"/>
</class>
</hibernate-mapping>
UserAccount.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 导入包名 -->
<hibernate-mapping package="cn.itcast.test.entity">
<!-- 配置类名并构建表 -->
<class name="UserAccount" table="t_useraccount">
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<property name="loginName" type="string" column="loginName"></property>
<property name="password" type="string" column="password"></property>
<!-- employee属性,表示与Employee的一对一关系 采用基于外键的映射方式,本方是有外键方 -->
<many-to-one name="employee" class="Employee" column="employeeId" unique="true" />
<!-- privileges属性,表示与Privilege的多对多 关系 -->
<set name="privileges" table="t_userAccount_privilege">
<key column="userAccountId" />
<many-to-many class="Privilege" column="privilegeId" />
</set>
</class>
</hibernate-mapping>
Privilege.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 导入包名 -->
<hibernate-mapping package="cn.itcast.test.entity">
<!-- 配置类名并构建表 -->
<class name="Privilege" table="t_privilege">
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<property name="action" type="string" column="action"></property>
<!-- userAccount属性,表示与UserAccount的多对多关系 -->
<set name="userAccount" table="t_userAccount_privilege">
<key column="privilegeId" />
<many-to-many class="UserAccount" column="userAccountId" />
</set>
</class>
</hibernate-mapping>