欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

精通Hibernate:对象关系映射基础(1)(3)

来源: javaer 分享于  点击 15356 次 点评:263

3、实例

本节的代码下载地址:http://down.51cto.com/data/326754

主要的BusinessService.java

  1. package mypack;  
  2.  
  3. import org.hibernate.*;  
  4. import org.hibernate.cfg.Configuration;  
  5. import java.util.*;  
  6.  
  7. public class BusinessService{  
  8.   public static SessionFactory sessionFactory;  
  9.   static{  
  10.      try{  
  11.        Configuration config = new Configuration()  
  12.              .setNamingStrategy( new MyNamingStrategy() )  
  13.              .configure();       //加载hibernate.cfg.xml文件中配置的信息  
  14.       sessionFactory = config.buildSessionFactory();  
  15.     }catch(RuntimeException e){e.printStackTrace();throw e;}  
  16.   }  
  17.  
  18.   public Customer loadCustomer(long customer_id){  
  19.     Session session = sessionFactory.openSession();  
  20.     Transaction tx = null;  
  21.     try {  
  22.       tx = session.beginTransaction();  
  23.       Customer customer=(Customer)session.get(Customer.class,new Long(customer_id));  
  24.       tx.commit();  
  25.       return customer;  
  26.     }catch (RuntimeException e) {  
  27.       if (tx != null) {  
  28.          tx.rollback();  
  29.       }  
  30.       throw e;  
  31.     } finally {  
  32.        session.close();  
  33.     }  
  34.   }  
  35.  
  36.   public void saveCustomer(Customer customer){  
  37.     Session session = sessionFactory.openSession();  
  38.     Transaction tx = null;  
  39.     try {  
  40.       tx = session.beginTransaction();  
  41.       session.save(customer);  
  42.       tx.commit();  
  43.  
  44.     }catch (RuntimeException e) {  
  45.       if (tx != null) {  
  46.          tx.rollback();  
  47.       }  
  48.       throw e;  
  49.     } finally {  
  50.        session.close();  
  51.     }  
  52.   }  
  53.  
  54.     public void loadAndUpdateCustomer(long customerId) {  
  55.       Session session = sessionFactory.openSession();  
  56.       Transaction tx = null;  
  57.       try {  
  58.         tx = session.beginTransaction();  
  59.         Customer customer=(Customer)session.get(Customer.class,new Long(customerId));  
  60.         customer.setDescription("A lovely customer!");  
  61.         tx.commit();  
  62.  
  63.     }catch (RuntimeException e) {  
  64.       if (tx != null) {  
  65.         tx.rollback();  
  66.       }  
  67.       throw e;  
  68.     } finally {  
  69.       session.close();  
  70.     }  
  71.   }  
  72.  
  73.   public void updateCustomer(Customer customer){  
  74.     Session session = sessionFactory.openSession();  
  75.     Transaction tx = null;  
  76.     try {  
  77.       tx = session.beginTransaction();  
  78.       session.update(customer);  
  79.       tx.commit();  
  80.  
  81.     }catch (RuntimeException e) {  
  82.       if (tx != null) {  
  83.          tx.rollback();  
  84.       }  
  85.       throw e;  
  86.     } finally {  
  87.        session.close();  
  88.     }  
  89.   }  
  90.  
  91.   public void saveDictionary(Dictionary dictionary) {  
  92.     Session session = sessionFactory.openSession();  
  93.     Transaction tx = null;  
  94.     try {  
  95.       tx = session.beginTransaction();  
  96.       session.save(dictionary);  
  97.       tx.commit();  
  98.  
  99.     }catch (RuntimeException e) {  
  100.       if (tx != null) {  
  101.         tx.rollback();  
  102.       }  
  103.       throw e;  
  104.     } finally {  
  105.       session.close();  
  106.     }  
  107.   }  
  108.  
  109.  public void updateDictionary(Dictionary dictionary){  
  110.     Session session = sessionFactory.openSession();  
  111.     Transaction tx = null;  
  112.     try {  
  113.       tx = session.beginTransaction();  
  114.       session.update(dictionary);  
  115.       tx.commit();  
  116.  
  117.     }catch (RuntimeException e) {  
  118.       if (tx != null) {  
  119.         tx.rollback();  
  120.       }  
  121.       throw e;  
  122.     } finally {  
  123.       session.close();  
  124.     }  
  125.   }  
  126.   public Dictionary loadDictionary(long dictionary_id) {  
  127.     Session session = sessionFactory.openSession();  
  128.     Transaction tx = null;  
  129.     try {  
  130.       tx = session.beginTransaction();  
  131.       Dictionary dictionary=(Dictionary)session.get(Dictionary.class,new Long(dictionary_id));  
  132.       tx.commit();  
  133.       return dictionary;  
  134.     }catch (RuntimeException e) {  
  135.       if (tx != null) {  
  136.         tx.rollback();  
  137.       }  
  138.       throw e;  
  139.     } finally {  
  140.       session.close();  
  141.     }  
  142.   }  
  143.  
  144.   public void printCustomer(Customer customer){  
  145.       System.out.println("name:"+customer.getName());  
  146.       System.out.println("sex:"+customer.getSex());  
  147.       System.out.println("description:"+customer.getDescription());  
  148.       System.out.println("avgPrice:"+customer.getAvgPrice());  
  149.       System.out.println("totalPrice:"+customer.getTotalPrice());  
  150.   }  
  151.  
  152.   public void printDictionary(Dictionary dictionary){  
  153.       System.out.println("type:"+dictionary.getType());  
  154.       System.out.println("key:"+dictionary.getKey());  
  155.       System.out.println("value:"+dictionary.getValue());  
  156.   }  
  157.    public void test(){  
  158.       Customer customer=new Customer("Laosan","Zhang",'M',new HashSet(),"A good citizen!");  
  159.       Order order1=new Order("Order001",new Double(100),customer);  
  160.       Order order2=new Order("Order002",new Double(200),customer);  
  161.       customer.getOrders().add(order1);  
  162.       customer.getOrders().add(order2);  
  163.  
  164.       saveCustomer(customer);  
  165.  
  166.       customer=new Customer("Laowu","Wang",'M',new HashSet(),null);  
  167.       saveCustomer(customer);  
  168.  
  169.       customer=loadCustomer(1);  
  170.       printCustomer(customer);  
  171.  
  172.       customer.setDescription("An honest customer!");  
  173.       updateCustomer(customer);  
  174.  
  175.       loadAndUpdateCustomer(1);  
  176.  
  177.       Dictionary dictionary=new Dictionary("SEX","M","MALE");  
  178.       saveDictionary(dictionary);  
  179.  
  180.       dictionary=loadDictionary(1);  
  181.       dictionary.setValue("MAN");  
  182.       updateDictionary(dictionary);  
  183.  
  184.       dictionary=loadDictionary(1);  
  185.       printDictionary(dictionary);  
  186.  
  187.    }  
  188.  
  189.   public static void main(String args[]) {  
  190.     new BusinessService().test();  
  191.     sessionFactory.close();  
  192.   }  


相关栏目:

用户点评