知识大全 hibernate关联关系-多对一

Posted 员工

篇首语:生活就像海洋,只有意志坚强的人,才能到达彼岸。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 hibernate关联关系-多对一相关的知识,希望对你有一定的参考价值。

  模型 员工Employee — 部门Department

  Java代码

  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;

  

  Java代码

  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代码

  <?xml version= ?>

  <!DOCTYPE hibernate mapping PUBLIC

   //Hibernate/Hibernate Mapping DTD //EN

   mapping 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代码

  <?xml version= ?>

  <!DOCTYPE hibernate mapping PUBLIC

   //Hibernate/Hibernate Mapping DTD //EN

   mapping 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>

  Java代码

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

  

  Java代码

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

  

  Java代码

  package Dao Impl;

  import hibernate HibernateException;

  import hibernate Query;

  import hibernate Session;

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

  mit();

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

  mit();

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

  mit();

  catch (HibernateException e)

  if(tx != null)

  tx rollback();

  

  throw e;

  finally

  if(s != null)

  s close();

  

  

  

  

  Java代码

  package Dao Impl;

  import hibernate HibernateException;

  import hibernate Query;

  import hibernate Session;

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

  mit();

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

  mit();

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

  mit();

  catch (HibernateException e)

  if(tx != null)

  tx rollback();

  

  throw e;

  finally

  if(s != null)

  s close();

  

  

  

  

  Java代码

  package Dao Test;

  import hibernate Session;

  import hibernate Transaction;

  import Utils hibernateUtil;

  import Domain Department;

  import Domain Employee;

  public class Many OneTest

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

  mit();

  return depart;

  finally

  if(s != null)

  s close();

  

  

  

cha138/Article/program/Java/ky/201311/28141

相关参考

知识大全 Hibernate的多对一和一对多操作实例

Hibernate的多对一和一对多操作实例  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Hib

知识大全 Hibernate多对多双向关联(xml配置)

Hibernate多对多双向关联(xml配置)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!   

知识大全 Hibernate各种映射关系总结

Hibernate各种映射关系总结  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  多对一  第一

知识大全 Hibernate一对一 主键关联映射

Hibernate一对一主键关联映射  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n

知识大全 Hibernate中多对多关系的常见问题

Hibernate中多对多关系的常见问题  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  1到底在

知识大全 Hibernate持久化技术中多对多关系应用

Hibernate持久化技术中多对多关系应用  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Hi

知识大全 hibernate关联关系-组件关联

  模型User—Name(两者同属一张表中)  Java代码  packageDomain;  importjavautilDate;  publicclassUser  publicintgetI

知识大全 hibernate的关联关系配置问题

  我现在有一个借阅信息类如下  classBorrow  privateStringborrowId;//借阅流水  privateBookbook;  privateUserborrowUser;

知识大全 hibernate一对多关联关系

  想了几天终于知道sql语句的发出问题查了很多书感觉都没有说清楚有的还是错的请看下面  [java]  <?xmlversion=?>  <!DOCTYPEhibernatemap

知识大全 hibernate关系映射

  一对象关系映射基础  对象间的基本关系  首先我简要阐明一下对象之间的基本关系在这以后UML的课程中也会深入的学习对象具有的四种基本关系  关联关系关联关系在设计模式中是被提倡优先使用于继承关系的