java

位置:IT落伍者 >> java >> 浏览文章

hibernate关联关系-多对一


发布日期:2018年10月16日
 
hibernate关联关系-多对一

模型员工Employee — 部门Department

Java代码

package Domain;

public class Employee {

public int getId() {

return id;

}

public void setId(int id) {

thisid = id;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

public Department getDepart() {

return depart;

}

public void setDepart(Department depart) {

thisdepart = depart;

}

private int id;

private String name;

private Department depart;

}

Java代码

package Domain;

import javautilSet;

public class Department {

public int getId() {

return id;

}

public void setId(int id) {

thisid = id;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

public Set<Employee> getEmps() {

return emps;

}

public void setEmps(Set<Employee> emps) {

thisemps = emps;

}

private int id;

private String name;

private Set<Employee> emps ;

}

Xml代码

<?xml version=?>

<!DOCTYPE hibernatemapping PUBLIC

//Hibernate/Hibernate Mapping DTD //EN

mappingdtd>

<hibernatemapping package=Domain>

<class name=Employee table=employee>

<id name=id>

<generator class=native />

</id>

<property name=name unique=true/>

<manytoone name=depart column=depart_id/>

</class>

</hibernatemapping>

Xml代码

<?xml version=?>

<!DOCTYPE hibernatemapping PUBLIC

//Hibernate/Hibernate Mapping DTD //EN

mappingdtd>

<hibernatemapping 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/>

<onetomany class=Employee/>

</set>

</class>

</hibernatemapping>

Java代码

package Dao;

import DomainEmployee;

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);

}

Java代码

package Dao;

import DomainDepartment;

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);

}

Java代码

package DaoImpl;

import orghibernateHibernateException;

import orghibernateQuery;

import orghibernateSession;

import orghibernateTransaction;

import UtilshibernateUtil;

import DaoEmployeeDAO;

import DomainEmployee;

public class EmployeeDAOImpl implements EmployeeDAO {

// 保存员工

public void saveEmployee(Employee emp) {

Session s = null;

Transaction tx = null;

try{

s = hibernateUtilgetSession();

tx = sbeginTransaction();

ssave(emp);

mit();

}catch (HibernateException e) {

if(tx != null){

txrollback();

}

throw e;

}finally{

if(s != null){

sclose();

}

}

}

// 根据姓名查询员工

public Employee findEmployeeByName(String name) {

Session s = null ;

try{

s = hibernateUtilgetSession();

String hql = from Employee as emp where empname=:name;

Query query = screateQuery(hql);

querysetString(name name);

Employee emp = (Employee) queryuniqueResult();

return emp;

}finally{

if(s != null){

sclose();

}

}

}

// 根据员工id查询员工

public Employee findEmployeeById(int id) {

Session s = null ;

try{

s = hibernateUtilgetSession();

Employee emp = (Employee) sget(Employeeclass id);

return emp;

}finally{

if(s != null)

{

sclose();

}

}

}

// 更新员工信息

public void updateEmployee(Employee emp) {

Session s = null;

Transaction tx = null;

try{

s = hibernateUtilgetSession();

tx = sbeginTransaction();

supdate(emp);

mit();

}catch (HibernateException e) {

if(tx != null){

txrollback();

}

throw e;

}finally{

if(s != null){

sclose();

}

}

}

// 删除员工

public void removeEmployee(Employee emp) {

Session s = null;

Transaction tx = null;

try{

s = hibernateUtilgetSession();

tx = sbeginTransaction();

sdelete(emp);

mit();

}catch (HibernateException e) {

if(tx != null){

txrollback();

}

throw e;

}finally{

if(s != null){

sclose();

}

}

}

}

Java代码

package DaoImpl;

import orghibernateHibernateException;

import orghibernateQuery;

import orghibernateSession;

import orghibernateTransaction;

import DaoDepartmentDAO;

import DomainDepartment;

import UtilshibernateUtil;

public class DepartmentDAOImpl implements DepartmentDAO {

// 保存部门

public void saveDepartment(Department depart) {

Session s = null;

Transaction tx = null;

try{

s = hibernateUtilgetSession();

tx = sbeginTransaction();

ssave(depart);

mit();

}catch (HibernateException e) {

if(tx != null){

txrollback();

}

throw e;

}finally{

if(s != null){

sclose();

}

}

}

// 根据name查找部门

public Department findDepartmentByName(String name) {

Session s = null ;

try{

s = hibernateUtilgetSession();

String hql = from Department as depart where departname=:name;

Query query = screateQuery(hql);

querysetString(name name);

Department depart = (Department) queryuniqueResult();

return depart;

}finally{

if(s != null){

sclose();

}

}

}

// 根据id查找部门

public Department findDepartmentById(int id) {

Session s = null ;

try{

s = hibernateUtilgetSession();

Department depart = (Department) sget(Departmentclass id);

return depart;

}finally{

if(s != null)

{

sclose();

}

}

}

// 更新部门

public void updateDepartment(Department depart) {

Session s = null;

Transaction tx = null;

try{

s = hibernateUtilgetSession();

tx = sbeginTransaction();

supdate(depart);

mit();

}catch (HibernateException e) {

if(tx != null){

txrollback();

}

throw e;

}finally{

if(s != null){

sclose();

}

}

}

// 删除部门

public void removeDepartment(Department depart) {

Session s = null;

Transaction tx = null;

try{

s = hibernateUtilgetSession();

tx = sbeginTransaction();

sdelete(depart);

mit();

}catch (HibernateException e) {

if(tx != null){

txrollback();

}

throw e;

}finally{

if(s != null){

sclose();

}

}

}

}

Java代码

package DaoTest;

import orghibernateSession;

import orghibernateTransaction;

import UtilshibernateUtil;

import DomainDepartment;

import DomainEmployee;

public class ManyOneTest {

public static void main(String[] args) {

add();

}

public static Department add(){

Session s = null ;

Transaction tx = null;

try{

Department depart = new Department();

departsetName(xuan chuan bu);

Employee emp = new Employee();

empsetDepart(depart);// 对象模型建立两个对象间的关联关系

empsetName(zhang zuoqiang);

s = hibernateUtilgetSession();

tx = sbeginTransaction();

ssave(depart);

ssave(emp);

mit();

return depart;

}finally{

if(s != null){

sclose();

}

}

}

}

               

上一篇:从Hibernate的映射谈编程思想

下一篇:hibernate级联删除