custserviceimpl.java

来自「网上商店 新增功能: 1」· Java 代码 · 共 89 行

JAVA
89
字号
package netstore.service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;

import netstore.businessobjects.*;

// Import the exceptions used
import netstore.framework.exceptions.DatastoreException;

import javax.servlet.ServletContext;

// Import the implementation specific packages
//hibernate 3.0
import org.hibernate.*;

import org.apache.log4j.*;

/**
 * 业务代理实现
 * @author huangyongfeng
 *
 */
public class CustServiceImpl extends NetstoreServiceImpl{
    private static Logger logger = Logger.getLogger(CustServiceImpl.class.getName());	
  
 
  /**
   * Create the service *
   */
  public CustServiceImpl() throws DatastoreException {
    super();
  }

  /**
   * 更新客户
   */
 public void saveOrUpdateCustomer(Customer customer) throws
    DatastoreException{

    Session session = HibernateUtil.getSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      //仅仅一句话
      //如果删除订单会级联删除啊!很好!
      session.saveOrUpdate(customer);
      tx.commit();

    }catch (Exception ex) {
      HibernateUtil.rollbackTransaction(tx);
      throw DatastoreException.datastoreError(ex);
    } finally {
      // No matter what, close the session
      HibernateUtil.closeSession(session);
    }
  }

  /**
   * 通过id获取客户
   */
  public Customer getCustomerById(Long id) throws DatastoreException{

    Session session = HibernateUtil.getSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      Query query = session.createQuery("from Customer c left outer join fetch c.orders where c.id=:id");
      query.setLong("id",id.longValue());
      Customer customer =(Customer) query
                          .uniqueResult();

      tx.commit();

      return customer;

    }catch (Exception ex) {
      HibernateUtil.rollbackTransaction(tx);
      throw DatastoreException.datastoreError(ex);
    } finally {
      // No matter what, close the session
      HibernateUtil.closeSession(session);
    }
  }
  
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?