Hey,
We are having problems with calling and using a hibernate method (in a seperate .java file) from a jsp.page.
We used the method getUsers which are located in the java file. This method uses Hibernate to communicate with the DB and return a list of users.
We tested this method through another java file to check it "internally" within eclipse. This worked fine. However when deploying and trying to access the method through the .jsp page, we got the following exception:
Code:
14:57:59,574 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
We have searched for good examples using JSP with Hibernate to no avail. Links to examples of this kind would be greatly appreciatedl.
Using JBOSS 4.0.3, Eclipse3.1.1 , PostgeSQL 8.0, Hibernate 3.0
Hibernate version:3.0 Mapping documents:Hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">xxxxxx</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:xxxx/XXXX</property>
<property name="hibernate.connection.username">xxxxx</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- mapping files -->
<mapping resource="UserGroup.hbm.xml" />
<mapping resource="Users.hbm.xml" />
<mapping resource="Module.hbm.xml" />
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():Code:
Session session = HibernateSessionFactory.currentSession();
//always start a transaction before doing something
// from the database
Transaction transaction = session.beginTransaction();
List users = session.createQuery("from Users").list();
for (Iterator iterator = users.iterator(); iterator.hasNext();) {
Users user = (Users) iterator.next();
System.out.println("Id " + user.getUserID() + " Name " + user.getFirstName());
}
// commit your transaction or nothing is wrote to the db
transaction.commit();
// lean up (close the session)
HibernateSessionFactory.closeSession();
Full stack trace of any exception that occurs:Code:
14:57:59,338 INFO [Configuration] configuring from resource: hibernate.cfg.xml
14:57:59,339 INFO [Configuration] Configuration resource: hibernate.cfg.xml
14:57:59,363 INFO [Configuration] Reading mappings from resource: Users.hbm.xml
14:57:59,419 INFO [HbmBinder] Mapping class: ValueObjects.Users -> users
14:57:59,531 INFO [Configuration] Configured SessionFactory: null
14:57:59,534 INFO [Configuration] processing extends queue
14:57:59,535 INFO [Configuration] processing collection mappings
14:57:59,536 INFO [Configuration] processing association property references
14:57:59,537 INFO [Configuration] processing foreign key constraints
14:57:59,538 INFO [DriverManagerConnectionProvider] Using Hibernate built-in connection pool (not for production use!)
14:57:59,539 INFO [DriverManagerConnectionProvider] Hibernate connection pool size: 20
14:57:59,539 INFO [DriverManagerConnectionProvider] autocommit mode: false
14:57:59,540 INFO [DriverManagerConnectionProvider] using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:5432/TRIM
14:57:59,541 INFO [DriverManagerConnectionProvider] connection properties: {user=admin, password=123456}
14:57:59,550 INFO [SettingsFactory] RDBMS: PostgreSQL, version: 8.0.4
14:57:59,550 INFO [SettingsFactory] JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.0 JDBC3 with SSL (build 314)
14:57:59,552 INFO [Dialect] Using dialect: org.hibernate.dialect.PostgreSQLDialect
14:57:59,558 INFO [TransactionFactoryFactory] Using default transaction strategy (direct JDBC transactions)
14:57:59,558 INFO [TransactionManagerLookupFactory] No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
14:57:59,559 INFO [SettingsFactory] Automatic flush during beforeCompletion(): disabled
14:57:59,560 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
14:57:59,561 INFO [SettingsFactory] JDBC batch size: 15
14:57:59,561 INFO [SettingsFactory] JDBC batch updates for versioned data: disabled
14:57:59,562 INFO [SettingsFactory] Scrollable result sets: enabled
14:57:59,563 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
14:57:59,563 INFO [SettingsFactory] Connection release mode: auto
14:57:59,564 INFO [SettingsFactory] Default batch fetch size: 1
14:57:59,565 INFO [SettingsFactory] Generate SQL with comments: disabled
14:57:59,566 INFO [SettingsFactory] Order SQL updates by primary key: disabled
14:57:59,567 INFO [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
14:57:59,568 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
14:57:59,569 INFO [SettingsFactory] Query language substitutions: {}
14:57:59,570 INFO [SettingsFactory] Second-level cache: enabled
14:57:59,570 INFO [SettingsFactory] Query cache: disabled
14:57:59,571 INFO [SettingsFactory] Cache provider: org.hibernate.cache.EhCacheProvider
14:57:59,574 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2328)
at java.lang.Class.getConstructor0(Class.java:2640)
at java.lang.Class.newInstance0(Class.java:321)
at java.lang.Class.newInstance(Class.java:303)
at org.hibernate.cfg.SettingsFactory.createCacheProvider(SettingsFactory.java:327)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:219)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1823)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1143)
at HandleUsers.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:52)
at HandleUsers.HandleUsersController.getUsers(HandleUsersController.java:54)
at org.apache.jsp.HandleUsers_jsp._jspService(org.apache.jsp.HandleUsers_jsp:126)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
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:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
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:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:595)
Name and version of the database you are using:postgrSQL 8.0
Our JSP pageCode:
<%@ page language="java"%>
<%@page import="java.util.*"%>
<%@page import="ValueObjects.*"%>
<%@page import="HandleUsers.*"%>
<%@ page import="java.util.List" %>
<% HandleUsersController handleUsersController = new HandleUsersController(); %>
<% List users = null; %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%
users = handleUsersController.getUsers();
// handleUsersController.getUsers();
//// Users user = (Users) iterator.next();%>
<tr bgcolor="#CCCCCC">
<td><% // user.getLastName()%></td>
<td><% //user.getFirstName()%></td>
<td><% // user.getGroupID()%></td>
<td><endre></td>
<td><slett></td>
</tr>
<%// } %>
</body>
</html>
Our HandleUserControllerCode:
package HandleUsers;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ValueObjects.*;
public class HandleUsersController {
public HandleUsersController() {
// HandleUsersController handleUsersController = new HandleUsersController();
}
public List getUsers() {
//log.info("listing Users");
try {
Session session = HibernateSessionFactory.currentSession();
Transaction transaction = session.beginTransaction();
List users = session.createQuery("from Users").list();
for (Iterator iterator = users.iterator(); iterator.hasNext();) {
Users user = (Users) iterator.next();
System.out.println("Id " + user.getUserID() + " Name " + user.getFirstName());
}
transaction.commit();
HibernateSessionFactory.closeSession();
return users;
} catch (HibernateException e) {
// problem with Hibernate happened
System.out.println("HIBERNATE EXCEPTION FRA HANDLEUSERCONTROLLER");
e.printStackTrace();
return null;
} catch (Exception m){
System.out.println("EN NFEIL FRA HANDSLEUSERSCONTROLLERENEN");
m.printStackTrace();
return null;
}
}
}