Hibernate version: 3.0
Mapping documents:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/focus_pancakes</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Update database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="com/pancakes/website/controller/form/Condition.hbm.xml"/>
<mapping resource="com/pancakes/website/controller/form/Menu.hbm.xml"/>
<mapping resource="com/pancakes/website/controller/form/Review.hbm.xml"/>
<mapping resource="com/pancakes/website/controller/form/User.hbm.xml"/>
<mapping resource="com/pancakes/website/controller/form/Section.hbm.xml"/>
<mapping resource="com/pancakes/website/controller/form/Category.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.pancakes.website.controller.form.SectionForm" table="Sections">
<id name="sid">
<generator class="native"/>
</id>
<property name="section"/>
<property name="seq" type="int"/>
<set name="menuitems" inverse="true" cascade="all-delete-orphan">
<key column="sid" on-delete="noaction"/>
<one-to-many class="com.pancakes.website.controller.form.MenuForm" not-found="ignore"/>
</set>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Pancakes at the Manor</display-name>
<description>Pancakes at the Manor</description>
<filter>
<filter-name>HibernateFilter</filter-name>
<filter-class>com.pancakes.website.HibernateFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HibernateFilter</filter-name>
<servlet-name>*.do</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/config/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
Code between sessionFactory.openSession() and session.close():Code:
package com.pancakes.website;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
public static Session getSession() {
Session s = (Session) threadSession.get();
// Open a new Session, if this thread has none yet
try {
if (s == null) {
s = sessionFactory.openSession();
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
return s;
}
public static void closeSession() {
try {
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen())
s.close();
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
}
public static void beginTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
}
public static void commitTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
try {
if ( tx != null && !tx.wasCommitted()
&& !tx.wasRolledBack() )
tx.commit();
threadTransaction.set(null);
} catch (HibernateException ex) {
rollbackTransaction();
throw new HibernateException(ex);
}
}
public static void rollbackTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted()
&& !tx.wasRolledBack() ) {
tx.rollback();
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
} finally {
closeSession();
}
}
}
Code:
package com.pancakes.website;
import java.io.IOException;
import javax.servlet.*;
/**
*
* @author Murray
*/
public class HibernateFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// There is actually no explicit "opening" of a Session, the
// first call to HibernateUtil.beginTransaction() in control
// logic (e.g. use case controller/event handler, or even a
// DAO factory) will get a fresh Session.
try {
chain.doFilter(request, response);
// Commit any pending database transaction.
HibernateUtil.commitTransaction();
} catch (RuntimeException ex) {
HibernateUtil.rollbackTransaction(); // Also closes the Session
// Just rollback and let others handle the exception, e.g. for display
throw ex;
} finally {
// No matter what happens, close the Session.
HibernateUtil.closeSession();
}
}
public void destroy() {
}
}
Code:
package com.pancakes.website.controller.action;
import com.pancakes.website.Focus;
import com.pancakes.website.HibernateUtil;
import com.pancakes.website.controller.form.SectionForm;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
*
* @author Murray
*/
public class SectionDelete extends Action {
/** Creates a new instance of MenuDelete */
public SectionDelete() {
super();
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String szsid = request.getParameter("sid");
Integer sid = Focus.string2Integer(szsid);
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
SectionForm sf = (SectionForm) session.get(SectionForm.class, sid);
if (sf != null)
session.delete(sf);
List result = session.createQuery("from com.pancakes.website.controller.form.SectionForm order by seq").list();
request.setAttribute(SectionForm.listName, result);
return mapping.findForward("Next");
}
}
Full stack trace of any exception that occurs:Code:
3/10/2005 11:43:04 org.apache.struts.action.RequestProcessor processException
WARNING: Unhandled Exception thrown: class org.hibernate.exception.GenericJDBCException
3/10/2005 11:43:04 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet action threw exception
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:1596)
at org.hibernate.loader.Loader.list(Loader.java:1577)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:395)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:844)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at com.pancakes.website.controller.action.SectionAction.execute(SectionAction.java:40)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:534)
Name and version of the database you are using:
MySQL 4.1
Problems:
Hi folks
Sorry to land all of this stuff but I've been struggling for about a week to get Hibernate working with the Struts filter thing. I had Hibernate working when I started and closed a session within an Action class but suffered the problem of accessing data objects after the session was closed. So I tried to implement filtering.
I have looked at numerous versions of HibernateUtils and HibernateFilter code and they all seem a little different. The Hibernate reference document gives a sample but this is not related to a servlet filter. The CaveatEmptor example looks entirely different and fails to implement functions required in other HibernateUtils, like get(), commit(), etc. I hunted on Google and found more versions but still they differed from the previous versions I had checked through.
I have also read through the majority of 'Hibernate in Action' and even this document gives cut down examples that don't include all of the code bits required to make the application work. When I check the source code referenced by the document it differs.
Anyways, I'm struggling.
Currently my application will read and display stuff from the database but when I try to delete a row from the 'Sections' table the row is not removed and I get a JDBC error. The record remains in some locked state until I restart my database server. I'm thinking this problem may be related to the cascade="all-delete-orphan" option I am using (as this was recently added). I coded this while reading through the 'Hibernate in Action' documentation so not quite sure what I might have done wrong.
I would really appreciate it if somebody could take a quick look over my config files and the two java files above and let me know if anything is really out of whack.
TIA
Murray