1.VO: Put in the PUB project, Pub.vo
Package com.cattsoft.demo4bss.pub.vo;
import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder;
Public class companyvo {/ ** identifier field * / private string id;
/ * Nullable Persistent Field * / Private String Name
/ ** full constructor * / private string create; private int currentpageno; public companyvo (string name) {this.id = ID; this.name = name;
/ ** Default constructor * / public companyvo () {}
/ ** minimal constructor * / public companyvo (string id) {this.id = id;}
Public string getId () {returnim.
Public void setid (string id) {this.id = id;}
Public string getname () {return this.name;
Public void setname (String name) {this.name = name;}
Public String Tostring () {Return New TostringBuilder (this) .append ("id", getId ()) .tostring ();
public boolean equals (Object other) {if return false ((other instanceof CompanyVO)!); CompanyVO castOther = (CompanyVO) other; return new EqualsBuilder () .append (this.getId (), castOther.getId ()) .isEquals ();
public int hashCode () {return new HashCodeBuilder () .append (getId ()) .toHashCode ();} public String getCreateDate () {return createDate;} public void setCreateDate (String createDate) {this.createDate = createDate;} public INT getCurrentPageno () {Return CurrentPageno;} public void setcurrentpageno (int currentpageno) {this.currentpageno = currentpageno;}} 2.Dao: By Middle, you can modify
Package com.cattsoft.new97.ejb.dao;
Import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import net.sf.hibernate.hibernate; import net.sf.hibernate.Query; import java.util. *; import net. sf.hibernate.hibension;
Import com.cattsoft.new97.ejb.pub.basedao; import com.cattsoft.new97.ejb.hibernate. *; import com.cattsoft.new97.ejb.util. *; import com.cattsoft.demo4bss.pub.util. *; import com.cattsoft.demo4bss.pub.err. *; import com.cattsoft.deemo4bss.pub.vo. *;
Public class companydao extends baseda {
Private static log log = logfactory.getlog (companyDao.class);
Public Companydao () throws sysException {}
Public void addcompany (companyvo vo) THROWS SYSEXCEPTION, APPEXCEPTION {Company Company = new company (); company.GetId ()); company.getname (vol.getname ()); try {session.save (company) FlushSession (); log.info ("flushsession"); // The log should be handled in the EJB layer, because there is no transaction in the DAO layer //log.info ("New Company Basic Information: Company Number" Company.Getid () ", Company name" company.getname ());} catch (hibernateException he) {throw new sysexception ("10009", he);} catch (sysexception se) {se.appendmsg ("companyDao.addcompany" ); Throw se;}} public void updatecompany (companyvo vo) throws sysexception {company company = null; try {company = (company) session.load (company.class, vol.getid ()); Company.setID (VO. GetId ()); Company.setName (VO.GETNAME ());} catch (hibernateException he) {// throws a business error. No need to handle, only for the user. // Throw new APPEXCEPTION ("10009", He);} try {session.saveorUpdate; FlushSession (); //log.info ("Modify Company Basic Information: Company Number" Company.GetId () " , Company name " company.getname ());} catch (he.printstacktrace (); throw new sysException (" 10010 ", he);}}
public void del (String companyId) throws SysException {Company v1 = null; try {v1 = (Company) session.load (Company.class, companyId);} catch (HibernateException he) {throw new SysException ( "10013", he) } Try {session.delete (V1); flushsession ();} catch (hibernateException he) {throw new sysexception ("10013", he);} catch (sysexception se) {se.appendmsg ("companiesDAO.DEL") Throw se;}}}
PUBLIC PAGEDISPLAY Querycompany (Companyvo Vo) THROWS SYSEXCEPTION {pageDisplay page = new pagedisplay ();
List lst = null; page.getCurrentPageno ()); // Get query total no; string queryString = "SELECT Count (Cust) from customer =" String querywhere = "where 1 = 1"; query query = NULL; if (Vo.get (). Length ()> 0) {querywhere = "and custom cc = '" Vo.get () "'";} if (Vo.getName (). Length ()> 0) {Querywhere = "and custom.custname like '%" Vo.getName () "%'";} query Query1 = null; querystring = querywhere;
try {query1 = session.createQuery (queryString);} catch (HibernateException he) {throw new SysException ( "findCust1", he);} catch (java.lang.NullPointerException ne) {throw new SysException ( "findCustNull", ne) } Try {page.SettotAlRecno (hibernateutil.getcount (query1));} catch (sysexception he) {he.appendmsg ("custdao.findcusts"); throw he;}
// get query accord page try {queryString = "select cust from Cust as cust" queryWhere; query = session.createQuery (queryString);} catch (HibernateException he) {throw new SysException ( "findCust2", he);} query .setFirstResult (page.getFirstRecNo ()); query.setMaxResults (page.getMaxNo ()); //page.setCurrentRecNo(new HibernateUtil (query) .getCount ()); try {lst = query.list (); flushSession ( );} Catch (he.printstacktrace (); throw new sysexception ("FindCust3", He);} page.setcurrentRecno (LST.SIZE ()); Page.SETLST (LST);
Return Page;}}
3. SessionBean: call DAO
Package com.cattsoft.new97.ejb.company;
Import javax.ejb. *; // import com.cattsoft.demo4bss.form.custform; import com.cattsoft.new97.ejb.dao. *; import com.cattsoft.new97.ejb.util. *; import com.cattsoft .new97.ejb.hibernate *;. import java.util.Map; import java.rmi *;. import javax.rmi *;. import javax.naming *;. import javax.transaction.UserTransaction; import com.cattsoft.demo4bss .pub.err. *; import com.cattsoft.new97.ejb.pub. *; import com.cattsoft.new97.ejb.util. *; import com.cattsoft.deemo4bss.pub.util. *; // import EjbCall Import org.log; import org.apache.commons.logging.logfactory; import com.cattsoft.demo4bss.pub.vo. *;
public class McompanyEJBBean implements SessionBean {SessionContext sessionContext; private static Log log = LogFactory.getLog (McompanyEJBBean.class); public void ejbCreate () throws CreateException {}
Public void ejbremove () {}
Public void ejbactivate () {}
Public void ejbpassiVate () {}
Public void setsessionContext (sessioncontext sessioncontext) {this.SessionContext = sessionContext;
public void addCompany (CompanyVO vo) throws SysException, AppException {try {HibernateSession.openSession (); CompanyDAO companyDAO = new CompanyDAO (); companyDAO.addCompany (vo); // test statement tests the following situation database link. Extended 20 seconds, you can see the link situation at the console. //Try th (}}}}} catch (sysexception se) {this.sessionContext.serollbackonly (); se.appendmsg ("companyejbbean.addcompany); throw se;} catch (APPPPTION SE) {this.SessionContext.serollbackOnly (); se.appendmsg ("companyejbbean.addcompany); throw se;} finally {hibernatesis }.closesis ();}}
public void updateCompany (CompanyVO vo) throws SysException, AppException {try {HibernateSession.openSession (); CompanyDAO companyDAO = new CompanyDAO (); companyDAO.updateCompany (vo);} catch (SysException e) {this.sessionContext.setRollbackOnly (); e.appendMsg ( "CompanyEJBBean.updateCompany"); throw e;} / ** catch (AppException ae) {this.sessionContext.setRollbackOnly (); ae.appendMsg ( "CompanyEJBBean.updateCompany"); throw ae;} * / finally {HibernateSession ion.closesession ();
}
public void delCompany (String id) throws SysException, AppException {/ ** @ todo Complete this method * / try {HibernateSession.openSession (); CompanyDAO companyDAO = new CompanyDAO (); companyDAO.del (id);} catch (SysException se ) {This.SessionContext.serollbackOnly (); se.appendmsg ("companyejbbean.del"); throw se;} finally {hibernateration.closesis ();
}
public PageDisplay findCompany (CompanyVO vo) throws SysException {PageDisplay p = null; try {HibernateSession.openSession (); CompanyDAO companyDAO = new CompanyDAO (); p = companyDAO.queryCompany (vo); return p;} catch (SysException e) { Log.debug (E.GetMessage ()); this.sessionContext.serollbackOnly (); E.Appendmsg ("companyejbbean.findcompany); throw e;} finally {hibernatesis }.closesis ();}}} 4.Web layer (}}}) ( Struts: Tomorrow