Hi, I'm doing an hons thesis using hibernate, so I am quite wet behind the ears at this sort of thing. I've checked the forums (by searching for classcast) and nothing relevant came up.
here is the problem:
2004-05-12 12:54:13 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException
at MarkCT.Actions.login(Actions.java:143)
at org.apache.jsp.login_jsp._jspService(login_jsp.java:72)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:211)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:805)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:696)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:605)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
at java.lang.Thread.run(Thread.java:536)
Which is a clascast exception.
Here is the code from actions.java:
package MarkCT;
import javax.sql.*;
import java.sql.*;
import java.util.Calendar;
import javax.naming.*;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import Objects.*;
import java.util.List;
public class Actions
{
private Session ses;
private Transaction tx;
private Configuration config;
private SessionFactory sf;
private List result;
private String userLogin = "a";
private String userType = "";
public Actions()
throws
ClassNotFoundException,
SQLException,
NamingException,
net.sf.hibernate.MappingException,
net.sf.hibernate.HibernateException
{
//These lines were added for Hibernate goodness!
config = new Configuration().addClass(Omni_User.class);
sf = config.buildSessionFactory();
ses = sf.openSession();
//tx = ses.beginTransaction();
}
private void initialise() throws ClassNotFoundException, SQLException, NamingException,
net.sf.hibernate.MappingException,
net.sf.hibernate.HibernateException
{
//These lines were added for Hibernate goodness!
config = new Configuration().addClass(Omni_User.class);
sf = config.buildSessionFactory();
ses = sf.openSession();
//tx = ses.beginTransaction();
}
public String getUserLogin()
{
return userLogin;
}
public String getUserType()
{
return userType;
}
public boolean login(String username, String password)
throws
ClassNotFoundException,
SQLException,
NamingException,
net.sf.hibernate.MappingException,
net.sf.hibernate.HibernateException
{
if ( !(ses.isOpen()) )
{
initialise();
}
//Should only ever be one person with matching username and password - no cycling needed.
//If there is a problem, not allowing them to log in will hardly solve it.
result = ses.createQuery("SELECT user.userType FROM Objects.Omni_User as user WHERE user.password = "+password+" and user.login = "+username).list();
if (result.isEmpty())
{
return false;
}
Omni_User omni =(Omni_User) (result.get(0));
userType = omni.getUserType();
userLogin = omni.getLogin();
return true;
}
Here is the Omni_User POJO:
package Objects;
public class Omni_User {
private String login;
private String userType;
private String name;
private String password;
private int id;
// ===============
public Omni_User() {
}
public String getLogin() {
return login;
}
public String getUserType() {
return userType;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public int getId(){
return id;
}
public void setLogin(String l) {
login = l;
}
public void setUserType(String u) {
userType = u;
}
public void setName(String n) {
name = n;
}
public void setPassword(String p) {
password = p;
}
public void setId(int i){
id = i;
}
public String toString() {
return "[Login] " + login +
"[Usertype] " + userType + "\n" +
"[Name] " + name +
"[Password]" + password +
"[id]" + id + "\n";
}
}
Here is the Mapping file (Omni_User):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Objects.Omni_User" table="omni_user">
<!-- An integer is our incrementing key. It's automatically
generated by Hibernate with the increment pattern. -->
<id name="id" type="integer" unsaved-value="null" >
<column name="id" sql-type="integer" not-null="true"/>
<generator class="increment"/>
</id>
<property name="login">
<column name="login" sql-type="varchar(50)" not-null="true"/>
</property>
<property name="userType">
<column name="user_type" sql-type="varchar(50)" not-null="true"/>
</property>
<property name="name">
<column name="name" sql-type="varchar(50)" not-null="true"/>
</property>
<property name="password">
<column name="password" sql-type="varchar(50)" not-null="true"/>
</property>
</class>
</hibernate-mapping>
Misc:
Windows 98
The hibernate version is 2.1
I am using a MySQL database - it is version 4.0.18
Tomkat is version 5.0 - well, pretty sure it is. Is there a simple way of determining the version?
.jar files:
Here are where my various .jar files are - I don't know if this is causing any problems, but if you see any that are poorly placed, do tell me!
in /webapps/thesis/web-inf/lib:
cglib-full-2.0.1.jar
commons-collections-2.1.jar
commons-logging-1.0.3.jar
dom.jar
dom4j-1.4.jar
ehcache-0.7.jar
hibernate2.jar
jaxen-full.jar
jaxp-api.jar
jdbc2_0-stdect.jar
jstl.jar
jta.jat
log4j-1.2.8.jar
odmg-3.0.jar
sax.jar
saxpath.jar
standard.jar
xalan-2.4.0.jar
xerces-2.4.0.jar
In tomcat/common/lib:
ant.jar
commons-collections.jar
commos-el.jar
commons-pool-1.1.jar
jasper-compiler.jar
jasper-runtime.jar
jmx.jar
jmx-remote.jar
jmx-remote-tools.jar
jsp-api.jar
mysql-connecter-jave-3.1.1-alpha-bin.jar
mysql-resourcefactory.jar
naming-common.jar
naming-factory.jar
naming-java.jar
naming-resources.jar
servlet.jar
tools.jar
And finally, in the classpath, I point to:
hibernate2.jar,
commons-logging-1.0.3.jar
servlet.jar
odmg-3.0.jar
xalan-2.4.0.jar
xerces-2.4.0.jar
jta.jar
cglib-full.jar
dom4j-1.4.jar
log4j-1.2.8.jar
In my c:\hibernate\hibernate2.1\lib directory.
the error-code zones in on this line:
Omni_User omni =(Omni_User) (result.get(0));
Which seems ok to me - and the database table is:
omni_user:
login - varchar 50
user_type - varchar 50
name - varchar 50
password - varchar 50
id - int
FINALLY, the whole mess DOES connect, because if I try and run my program with a dodgy login/password combo, the false is returned. It seems to be a casting problem.
Sorry if I've posted too much data.
|