-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: java.lang.ClassCastException: [Ljava.lang.Object;
PostPosted: Tue Oct 04, 2005 1:24 pm 
Newbie

Joined: Tue Oct 04, 2005 1:18 pm
Posts: 2
Hi,

have anyone had problems with casting to table class from iterator?

Hibernate version: version 3.0.5, 25 May 2005

Mapping documents: Login.hbm.xml

<?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="sample.LoginTable" table="login">
<id name="username" column="username" type="java.lang.String"/>
<property name="pass" column="pass" type="java.lang.String" not-null="true" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

...

Session sessionHibernate = HibernateUtil.currentSession();
HttpSession session = aRequest.getSession();
LoginFormBean lBean = (LoginFormBean) aForm;
Integer nLoginAttempts = 0;
String lUsername = lBean.getUsername();
String lPassword = lBean.getPassword();
nLoginAttempts = (Integer) session.getAttribute("LoginAttempts");
if (nLoginAttempts == null) nLoginAttempts = 0;
if (nLoginAttempts.intValue() == 9) return (aMapping.findForward("logging_finished"));

tx = sessionHibernate.beginTransaction();
Query query = sessionHibernate.createQuery("select username,pass from " + LoginTable.class.getName() + " where username = :uname and pass = :pwd");
query.setString("uname", lUsername);
query.setString("pwd", lPassword);
Iterator it = query.iterate();

if ( it.hasNext() )
{
Problem --> LoginTable lt = (LoginTable)it.next();

if ( lt.getUsername().equalsIgnoreCase(lUsername) )
{
session.setAttribute("UserLogged", "true");

return (aMapping.findForward("logged"));
}
else
{
logger.info("LoginAttempt: " + nLoginAttempts);
nLoginAttempts++;
session.setAttribute("LoginAttempts", nLoginAttempts);

if (nLoginAttempts.intValue() == 9) return (aMapping.findForward("logging_finished"));
else return (aMapping.findForward("logging_error"));
}
...


Full stack trace of any exception that occurs:

javax.servlet.ServletException: ClassCastException: java.lang.ClassCastException: [Ljava.lang.Object;
at sample.LoginFormAction.execute(LoginFormAction.java:84)
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.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
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.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
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: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.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)


Name and version of the database you are using: Firebird v. 1.5

The generated SQL (show_sql=true):

Hibernate: select logintable0_.username as col_0_0_, logintable0_.pass as col_1_0_ from login logintable0_ where logintable0_.username=? and logintable0_.pass=?


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 3:27 pm 
Beginner
Beginner

Joined: Tue Jun 29, 2004 12:35 pm
Posts: 21
Quote:
Iterator it = query.iterate();

if ( it.hasNext() )
{
Problem --> LoginTable lt = (LoginTable)it.next();


Your query returns Object[] not LoginTable. Object[0] is username and [1] pw.

Use for example List<Object[]> data = (Object[])query.list();

--PO


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 3:39 pm 
Beginner
Beginner

Joined: Tue Jun 29, 2004 12:35 pm
Posts: 21
drooler wrote:
Quote:
Iterator it = query.iterate();

if ( it.hasNext() )
{
Problem --> LoginTable lt = (LoginTable)it.next();


Your query returns Object[] not LoginTable. Object[0] is username and [1] pw.

Use for example List<Object[]> data = (Object[])query.list();

--PO


Oops meant List<Object[]> data = query.list(); if using jdk 1.5 lol


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 3:41 pm 
Newbie

Joined: Tue Oct 04, 2005 1:18 pm
Posts: 2
Ok,

but what is then this example from hibernate doc?

Transaction tx = session.beginTransaction();
Query query = session.createQuery("select c from Cat as c where c.sex = :sex");
query.setCharacter("sex", 'F');
for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
out.println("Female Cat: " + cat.getName() );
}
tx.commit();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 3:49 pm 
Beginner
Beginner

Joined: Tue Jun 29, 2004 12:35 pm
Posts: 21
bcavlin wrote:
Ok,

but what is then this example from hibernate doc?

Transaction tx = session.beginTransaction();
Query query = session.createQuery("select c from Cat as c where c.sex = :sex");
query.setCharacter("sex", 'F');
for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
out.println("Female Cat: " + cat.getName() );
}
tx.commit();


Returns and instantiates Cat class wholly. If you want return instance from LoginTable use forexample query like this:

"select aLoginTable from LoginTable as a LoginTable where..."

returns list or iterator to LoginTable objects.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 3:51 pm 
Beginner
Beginner

Joined: Tue Jun 29, 2004 12:35 pm
Posts: 21
drooler wrote:
bcavlin wrote:
Ok,

but what is then this example from hibernate doc?

Transaction tx = session.beginTransaction();
Query query = session.createQuery("select c from Cat as c where c.sex = :sex");
query.setCharacter("sex", 'F');
for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
out.println("Female Cat: " + cat.getName() );
}
tx.commit();


Damn typos.. lol

Returns and instantiates Cat class wholly. If you want return instance from LoginTable use forexample query like this:

"select aLoginTable from LoginTable as aLoginTable where..."

returns list or iterator to LoginTable objects.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.