天天看點

DAO層開發小結1(注釋版)

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);

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);

    }

  }

  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);

    }

  }

  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);

    }

  }

}