OrgTest4.java
上传用户:kimgenplus
上传日期:2016-06-05
资源大小:20877k
文件大小:2k
源码类别:

OA系统

开发平台:

Java

  1. package com.bjsxt.oa.model;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.Set;
  5. import junit.framework.TestCase;
  6. import org.hibernate.Session;
  7. public class OrgTest4 extends TestCase {
  8. public void testSaveOrg(){
  9. Session session = HibernateUtils.getSession();
  10. try{
  11. session.beginTransaction();
  12. Organization org1 = new Organization();
  13. org1.setName("org1");
  14. session.save(org1);
  15. Organization org2 = new Organization();
  16. org2.setName("org2");
  17. org2.setParent(org1);
  18. session.save(org2);
  19. Organization org3 = new Organization();
  20. org3.setName("org3");
  21. org3.setParent(org1);
  22. session.save(org3);
  23. Organization org4 = new Organization();
  24. org4.setName("org4");
  25. org4.setParent(org1);
  26. session.save(org4);
  27. session.getTransaction().commit();
  28. }catch(Exception e){
  29. session.getTransaction().rollback();
  30. }finally{
  31. HibernateUtils.closeSession(session);
  32. }
  33. }
  34. public void testLoadOrg_01(){
  35. Session session = HibernateUtils.getSession();
  36. try{
  37. session.beginTransaction();
  38. //parent --> child
  39. Organization org1 = (Organization)session.load(Organization.class, 1);
  40. System.out.println(org1.getName()+" has children:");
  41. Set set = org1.getChildren();
  42. for (Iterator iter = set.iterator(); iter.hasNext();) {
  43. Organization org = (Organization) iter.next();
  44. System.out.print(org.getName());
  45. }
  46. session.getTransaction().commit();
  47. }catch(Exception e){
  48. session.getTransaction().rollback();
  49. }finally{
  50. HibernateUtils.closeSession(session);
  51. }
  52. }
  53. public void testLoadOrg_02(){
  54. Session session = HibernateUtils.getSession();
  55. try{
  56. session.beginTransaction();
  57. //child --> parent
  58. Organization org2 = (Organization)session.load(Organization.class, 2);
  59. System.out.println(org2.getName()+","+org2.getParent().getName());
  60. session.getTransaction().commit();
  61. }catch(Exception e){
  62. session.getTransaction().rollback();
  63. }finally{
  64. HibernateUtils.closeSession(session);
  65. }
  66. }
  67. }