DAO层开发小结1(注释版)

来源:互联网 发布:李宇春整容 知乎 编辑:程序博客网 时间:2024/05/01 22:47

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.Query;
import java.util.*;
import net.sf.hibernate.HibernateException;
import new97.pub.base.*;
import new97.pub.util.*;
import new97.pub.err.*;
import new97.ejb.po.*;
import new97.ejb.vo.*;

public class PartyDAO
    extends BaseDAO {
  private static Log log = LogFactory.getLog(PartyDAO.class);

 /**
   * 增加参与者
   * @param vo PartyVO
   * @throws SysException
   * @throws AppException
   */

public void createParty(PartyVO vo) throws
      SysException, AppException {

    Party party = new Party();   //新建一个po对象
    party.setCreateDate(vo.getCreateDate()); 
    party.setAddress(vo.getAddress()); // 从vo中读取所有的属性(对应数据库相关表)存入po对象,略
       try {
      CertType certType = (CertType) session.load(CertType.class, vo.getCertType());  根据vo转换成po对象

 party.setCertType(certType);  //在po中set方法中参数,即po的属性是一个对象的,需要这样做,以下同
      Area area = (Area) session.load(Area.class,vo.getArea());
      party.setArea(area);
      LocalNet localNet = (LocalNet) session.load(LocalNet.class,vo.getLocalNet());
      party.setLocalNet(localNet);
    }
    catch (HibernateException he) {
      he.printStackTrace();
      //抛出业务错误。不需要处理,仅给用户参考即可。
      throw new AppException("80001", he);
    }
    try {
      session.save(party);  //hibernate 的操作:保存
      session.flush();
      if (log.isDebugEnabled()) {
        log.debug("flushSession");
      }
    }
    catch (HibernateException he) {
      throw new SysException("10009", he);
    }
  }


  /**
   * 修改参与者
   * @param vo PartyVO
   * @throws SysException
   * @throws AppException
   */

  public void updateParty(PartyVO vo) throws
      SysException, AppException {
    Party party = null;   //新建一个po对象
    try {
      party = (Party) session.load(Party.class,
                                   vo.getPartyId());   //根据vo中的id,通过hibernate把表load出来到po对象中
      party.setCreateDate(vo.getCreateDate());  //根据vo,把修改后的数据set到po对象中,略
      party.setAddress(vo.getAddress());
          }
    catch (HibernateException he) {
      //抛出业务错误。不需要处理,仅给用户参考即可。
      throw new AppException("80002", he);
    }
    try {
      CertType certType = (CertType) session.load(CertType.class,
          vo.getCertType());
      party.setCertType(certType);  //在po中set方法中参数,即po的属性是一个对象的,需要这样做,以下同
      Area area = (Area) session.load(Area.class,vo.getArea());
      party.setArea(area);
      LocalNet localNet = (LocalNet) session.load(LocalNet.class,vo.getLocalNet());
      party.setLocalNet(localNet);
    }
    catch (HibernateException he) {
      he.printStackTrace();
      //抛出业务错误。不需要处理,仅给用户参考即可。
      throw new AppException("80001", he);
    }

    try {
      session.saveOrUpdate(party); //hibernate中的操作:修改后保存
      session.flush();
    }
    catch (HibernateException he) {
      he.printStackTrace();
      throw new SysException("10010", he);
    }

  }

 

 /**
   * 删除参与者
   * @param vo PartyVO
   * @throws SysException
   * @throws AppException   */

  public void deleteParty(Long partyId) throws SysException {
    Party v1 = null;  //新建po对象
    try {
      v1 = (Party) session.load(Party.class, partyId);  //根据参数id用hibernate把表来load到po中
    }
    catch (HibernateException he) {
      throw new SysException("10013", he);
    }
    try {
      session.delete(v1);  //hibernate的操作 : 删除表
      session.flush();
    }
    catch (HibernateException he) {
      throw new SysException("10013", he);
    }
  }

}

 

 

原创粉丝点击