多对一关联关系:
模型:员工Employee — 部门Department
package Domain;
public class Employee {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepart() {
return depart;
}
public void setDepart(Department depart) {
this.depart = depart;
}
private int id;
private String name;
private Department depart;
}
package Domain;
import java.util.Set;
public class Department {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this.emps = emps;
}
private int id;
private String name;
private Set<Employee> emps ;
}
<?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="Domain"> <class name="Employee" table="employee"> <id name="id"> <generator class="native" /> </id> <property name="name" unique="true"/> <many-to-one name="depart" column="depart_id"/> </class> </hibernate-mapping>
<?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="Domain"> <class name="Department" table="department"> <id name="id"> <generator class="native" /> </id> <property name="name" unique="true"/> <set name="emps"> <key column="depart_id"/> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
package Dao;
import Domain.Employee;
public interface EmployeeDAO {
public void saveEmployee(Employee emp);
public Employee findEmployeeByName(String name);
public Employee findEmployeeById(int id);
public void updateEmployee(Employee emp);
public void removeEmployee(Employee emp);
}
package Dao;
import Domain.Department;
public interface DepartmentDAO {
public void saveDepartment(Department depart);
public Department findDepartmentByName(String name);
public Department findDepartmentById(int id);
public void updateDepartment(Department depart);
public void removeDepartment(Department depart);
}
package Dao.Impl;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import Utils.hibernateUtil;
import Dao.EmployeeDAO;
import Domain.Employee;
public class EmployeeDAOImpl implements EmployeeDAO {
// 保存员工
public void saveEmployee(Employee emp) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.save(emp);
tx.commit();
}catch (HibernateException e) {
if(tx != null){
tx.rollback();
}
throw e;
}finally{
if(s != null){
s.close();
}
}
}
// 根据姓名查询员工
public Employee findEmployeeByName(String name) {
Session s = null ;
try{
s = hibernateUtil.getSession();
String hql = "from Employee as emp where emp.name=:name";
Query query = s.createQuery(hql);
query.setString("name", name);
Employee emp = (Employee) query.uniqueResult();
return emp;
}finally{
if(s != null){
s.close();
}
}
}
// 根据员工id查询员工
public Employee findEmployeeById(int id) {
Session s = null ;
try{
s = hibernateUtil.getSession();
Employee emp = (Employee) s.get(Employee.class, id);
return emp;
}finally{
if(s != null)
{
s.close();
}
}
}
// 更新员工信息
public void updateEmployee(Employee emp) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.update(emp);
tx.commit();
}catch (HibernateException e) {
if(tx != null){
tx.rollback();
}
throw e;
}finally{
if(s != null){
s.close();
}
}
}
// 删除员工
public void removeEmployee(Employee emp) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.delete(emp);
tx.commit();
}catch (HibernateException e) {
if(tx != null){
tx.rollback();
}
throw e;
}finally{
if(s != null){
s.close();
}
}
}
}
package Dao.Impl;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import Dao.DepartmentDAO;
import Domain.Department;
import Utils.hibernateUtil;
public class DepartmentDAOImpl implements DepartmentDAO {
// 保存部门
public void saveDepartment(Department depart) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.save(depart);
tx.commit();
}catch (HibernateException e) {
if(tx != null){
tx.rollback();
}
throw e;
}finally{
if(s != null){
s.close();
}
}
}
// 根据name查找部门
public Department findDepartmentByName(String name) {
Session s = null ;
try{
s = hibernateUtil.getSession();
String hql = "from Department as depart where depart.name=:name";
Query query = s.createQuery(hql);
query.setString("name", name);
Department depart = (Department) query.uniqueResult();
return depart;
}finally{
if(s != null){
s.close();
}
}
}
// 根据id查找部门
public Department findDepartmentById(int id) {
Session s = null ;
try{
s = hibernateUtil.getSession();
Department depart = (Department) s.get(Department.class, id);
return depart;
}finally{
if(s != null)
{
s.close();
}
}
}
// 更新部门
public void updateDepartment(Department depart) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.update(depart);
tx.commit();
}catch (HibernateException e) {
if(tx != null){
tx.rollback();
}
throw e;
}finally{
if(s != null){
s.close();
}
}
}
// 删除部门
public void removeDepartment(Department depart) {
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.delete(depart);
tx.commit();
}catch (HibernateException e) {
if(tx != null){
tx.rollback();
}
throw e;
}finally{
if(s != null){
s.close();
}
}
}
}
package Dao.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import Utils.hibernateUtil;
import Domain.Department;
import Domain.Employee;
public class Many2OneTest {
public static void main(String[] args) {
add();
}
public static Department add(){
Session s = null ;
Transaction tx = null;
try{
Department depart = new Department();
depart.setName("xuan chuan bu");
Employee emp = new Employee();
emp.setDepart(depart);// 对象模型,建立两个对象间的关联关系
emp.setName("zhang zuoqiang");
s = hibernateUtil.getSession();
tx = s.beginTransaction();
s.save(depart);
s.save(emp);
tx.commit();
return depart;
}finally{
if(s != null){
s.close();
}
}
}
}