落葉J空間

          常用鏈接

          統計

          最新評論

          我的第一個hibernate(詳解)-3

          最后建立類 BusinessService.java 放置 src/com

          package com;

          ?

          ?

          ?

          //import net.sf.hibernate.*;

          import net.sf.hibernate.*;

          import net.sf.hibernate.SessionFactory;

          import net.sf.hibernate.cfg.Configuration;

          import java.util.*;

          //import net.sf.hibernate.cfg.Configuration;

          import java.util.*;

          ?

          ?

          ?

          public class BusinessService{

          ? public static SessionFactory sessionFactory;

          ? static{

          ???? try{

          ????? // Create a configuration based on the properties file we've put

          ?????? 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 List findOrdersByCustomer(Customer customer) throws Exception{

          ??? Session session = sessionFactory.openSession();

          ??? Transaction tx = null;

          ??? try {

          ????? tx = session.beginTransaction();

          ?

          ????? List orders=(List)session.find("from Order as o where o.customer.id="+customer.getId());

          ????? tx.commit();

          ????? return orders;

          ??? }catch (Exception e) {

          ????? if (tx != null) {

          ??????? tx.rollback();

          ????? }

          ????? throw e;

          ??? } finally {

          ????? session.close();

          ??? }

          ? }

          ?

          ? public Customer findCustomer(long customer_id) throws Exception{

          ??? Session session = sessionFactory.openSession();

          ??? Transaction tx = null;

          ??? try {

          ????? tx = session.beginTransaction();

          ????? Customer customer=(Customer)session.load(Customer.class,new Long(customer_id));

          ????? 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 saveCustomerAndOrderWithCascade() throws Exception{

          ??? Session session = sessionFactory.openSession();

          ??? Transaction tx = null;

          ??? try {

          ????? tx = session.beginTransaction();

          ?

          ????? Customer customer=new Customer("Jack");

          ????? Order order1=new Order("Jack_Order001",customer);

          ????? Order order2=new Order("Jack_Order002",customer);

          ?

          ????? session.save(order1);

          ????? session.save(order2);

          ?

          ????? tx.commit();

          ?

          ??? }catch (Exception e) {

          ????? if (tx != null) {

          ??????? tx.rollback();

          ????? }

          ????? e.printStackTrace();

          ??? } finally {

          ????? // No matter what, close the session

          ????? session.close();

          ??? }

          ? }

          ?

          ? public void saveCustomerAndOrder() 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=new Customer("Tom");

          ?????? ?? session.save(customer);

          ?????? ?

          ??? Order order1=new Order("Tom_Order001",customer);

          ???? Order order2=new Order("Tom_Order002",customer);

          ??? session.save(order1);

          ???? session.save(order2);

          ????? // 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 void printOrders(List orders){

          ????? for (Iterator it = orders.iterator(); it.hasNext();) {

          ???????? Order order=(Order)it.next();

          ???????? System.out.println("OrderNumber of "+order.getCustomer().getName()+ " :"+order.getOrderNumber());

          ????? }

          ? }

          ?

          ?? public void test() throws Exception{

          ????? //saveCustomerAndOrder();

          ???? // saveCustomerAndOrderWithCascade();

          ????? Customer customer=findCustomer(1);

          ????? List orders=findOrdersByCustomer(customer);

          ???? printOrders(orders);

          ? }

          ?

          ? public static void main(String args[]) throws Exception {

          ??? new BusinessService().test();

          ??? sessionFactory.close();

          ? }

          }

          運行 : BusinessService.java

          在文件 BusinessService.java 點擊右建


          運行過程如下




          查看數據庫 就可看到數據庫中添加了相關記錄



          因為我也是初學 如果還有什么問題 可以和我聯系 一起學習

          我的 QQ397125569

          posted on 2006-08-09 18:13 黃暉 閱讀(110) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 米易县| 荔浦县| 福泉市| 张北县| 平远县| 黄骅市| 东安县| 台北市| 新河县| 孙吴县| 湘潭县| 新干县| 南城县| 即墨市| 沁水县| 东乡| 大荔县| 当阳市| 永丰县| 涞源县| 思南县| 凤城市| 油尖旺区| 丹巴县| 五常市| 红河县| 徐汇区| 临沧市| 胶南市| 丹巴县| 吕梁市| 鹿邑县| 丰都县| 韩城市| 宁波市| 沅江市| 彭山县| 吴堡县| 习水县| 陆良县| 武胜县|