⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contactlist3.java

📁 java对象/关系数据库映射工具hibernate和java web 框架struts结合的实例
💻 JAVA
字号:
//$Id: ContactList3.java,v 1.1 2003/03/14 22:00:26 thusted Exp $
package eg1;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import struts.HibernatePlugIn;
import eg.User;


/**
 * Load example.
 *
 * An example of a DispatchAction that, in each session, reloads persistent
 * objects before manipulating them. This is a very similar approach
 * to EJB entity beans. The disadvantage of this approach is that
 * isolation of business processes that span multiple sessions is
 * the responsibility of the application (ie. it should do its own
 * version checking). This is the least efficient approach in terms
 * of database accesses because all persistent state must be reloaded
 * from the database in each Session.
 *
 * It is the most efficient approach in terms of use of the
 * servlet session context, since it only needs to retain the
 * user id rather than a user object.
 *
 */
public class ContactList3 extends ContactList {

    public Session obtainSession(HttpServletRequest request) throws HibernateException {
        return HibernatePlugIn.open(request);
    }

    public void releaseSession(Session session) throws HibernateException {
        // closed in finally
    }

    public void doException(Exception e, HttpServletRequest request, Session session) throws Exception {
        // the session threw an exception, possibly leaving it
        // in an invalid state
        request.getSession().invalidate();
        if (null != session) {
            session.connection().rollback();
        }
        throw e;
    }

    public void doFinally(Session session) throws HibernateException {
        // Since the session is not cached, we can close it every time
        if (null != session) session.close();
    }

    // -- DISPATCH METHODS --

    public void doLoad(ActionForm form, HttpServletRequest request) throws HibernateException {
        EgForm egForm = (EgForm) form;
        User user = getUser(request);
        egForm.getSession().load(user, user.getId()); // reload entity from database
    }

    public ActionForward update(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {

        doLoad(form, request);
        ActionForward forward = super.update(mapping, form, request, response);
        return forward;
    }

    public ActionForward delete(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {

        doLoad(form, request);
        return super.delete(mapping, form, request, response);
    }

    public ActionForward addEntry(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {

        ActionForward forward = super.addEntry(mapping, form, request, response);
        doLoad(form, request);
        return forward;
    }

    public ActionForward deleteEntry(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {

        doLoad(form, request);
        return super.deleteEntry(mapping, form, request, response);
    }

}

⌨️ 快捷键说明

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