熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

hibernate關聯關系-多對一

2022-06-13   來源: Java開源技術 

  模型員工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();

  }

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28141.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.