首页>代码>精通hibernate源码>/精通hibernate源码/appendixC/BusinessService.java
package mypack;

import java.lang.reflect.Constructor;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import java.io.*;
import java.sql.Time;
import java.util.*;

public class BusinessService{
  public static SessionFactory sessionFactory;
  static{
     try{
      // Create a configuration based on the properties file we've put
      // in the standard place.
      Configuration config = new Configuration();
      config.addClass(Customer.class)
            .addClass(Order.class);
      // Get the session factory we can use for persistence
      sessionFactory = config.buildSessionFactory();
    }catch(Exception e){e.printStackTrace();}
  }

  public void saveCustomer(Customer customer) throws Exception{
  	  // Ask for a session using the JDBC information we've configured
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      session.save(customer);
      // We're done; make our changes permanent
      tx.commit();

    }catch (Exception e) {
      if (tx != null) {
        // Something went wrong; discard all partial changes
        tx.rollback();
      }
      throw e;
    } finally {
      // No matter what, close the session
      session.close();
    }
  }

  public Customer loadCustomer(Long id) throws Exception{
  	  // Ask for a session using the JDBC information we've configured
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      Customer customer=(Customer)session.load(Customer.class,id);
      // We're done; make our changes permanent
      tx.commit();
      return customer;

    }catch (Exception e) {
      if (tx != null) {
        // Something went wrong; discard all partial changes
        tx.rollback();
      }
      throw e;
    } finally {
      // No matter what, close the session
      session.close();
    }
  }

  public void printCustomer(Customer customer){
    Address address=customer.getAddress();
    Order order=customer.getOrder();
    System.out.println("Name:"+customer.getName());
    System.out.println("Address:"+address.getStreet()+" "    +address.getCity()+"+address.getProvice()+" "+address.getZipcode());
   System.out.println("OrderNumber:"+order.getOrderNumber());
  }
  public void test() throws Exception{

    Customer customer=new Customer();
    customer.setName("Tom");
    Address address=new Address("Stree1","City1","Province1","100001");
    Order order=new Order("Tom_Order001",customer); 
    saveCustomer(customer);
    customer=loadCustomer(customer.getId());
    printCustomer(customer);
  }

  public static void main(String args[]) throws Exception {
    new BusinessService().test();
    sessionFactory.close();
  }

}
最近下载更多
maqiang2020  LV1 2020年6月20日
cz206616  LV10 2020年6月4日
dengjunjun  LV15 2020年4月13日
caozhou  LV14 2019年3月11日
beihai98  LV1 2018年11月1日
zhaolihuiziyu  LV1 2018年9月7日
1792312911  LV17 2018年6月26日
hongzhangzhao  LV1 2017年11月17日
lwf626888  LV16 2017年9月19日
jason0943  LV11 2017年6月21日
最近浏览更多
liang85  LV1 2023年6月30日
微信网友_6186189978783744 2022年10月22日
暂无贡献等级
gxlgxl  LV4 2022年7月7日
SHUFONG  LV2 2021年12月16日
chinawind1990  LV5 2021年11月25日
blueskyroof  LV9 2021年1月12日
lt33333  LV7 2020年10月29日
南方者 2020年9月7日
暂无贡献等级
maqiang2020  LV1 2020年6月20日
cz206616  LV10 2020年6月4日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友