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

📄 contactlist2.java

📁 java对象/关系数据库映射工具hibernate和java web 框架struts结合的实例
💻 JAVA
字号:
//$Id: ContactList2.java,v 1.2 2003/03/14 20:04:28 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;


/**
 * Update example.
 *
 * An example of a DispatchAction that uses Session.update() to update
 * persistent state.
 *
 * This subclass only overrides the behavior that changes from the
 * "disconnect example" (ContactList).
 *
 * This example opens and closes the session, rather than
 * use reconnect and disconnect.
 * Since the session is not retained, the dispatch methods need to call
 * update in order to reassociate the User runtime object with the User
 * entity object.
 * The modified state of the User instance (including its Contacts)
 * will then be made persistent.
 *
 * Isolation of business processes that span multiple Sessions is
 * achieved by checking version numbers held by the updating transient
 * instances against the persistent version numbers.
 */
public class ContactList2 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 doUpdate(ActionForm form, HttpServletRequest request) throws HibernateException {
        EgForm egForm = (EgForm) form;
        egForm.getSession().update(getUser(request)); // reassociate with entity
    }

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

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

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

        doUpdate(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);
        doUpdate(form, request);
        return forward;
    }

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

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

}

⌨️ 快捷键说明

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