-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: class cast exception while casting the list object to entity
PostPosted: Tue May 22, 2007 6:32 am 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
class cast exception occurs while type casting the list object which is return from the hibernate to the entity class type.


Hibernate version: 3.1

Mapping documents:

accountgroup.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.DAO.account.Accountgroup" table="accountgroup" catalog="pao">
<id name="accgrpid" type="java.lang.String">
<column name="ACCGRPID" length="4" />
<generator class="native" />
</id>
<property name="accgrpdesc" type="java.lang.String">
<column name="ACCGRPDESC" length="30" />
</property>
<property name="standard" type="java.lang.Short">
<column name="STANDARD" />
</property>
</class>
</hibernate-mapping>


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

public Account find(String query ,HttpServletRequest request)

{
Session session;
Account result;

ServletContext context=request.getSession().getServletContext();
SessionFactory _factory = (SessionFactory) context.getAttribute("SESSION_FACTORY_KEY_CRM");
session=_factory.openSession();

result = (Account) (session.createSQLQuery(query)).list(); // class cast exception occurs here

return result;



}





Full stack trace of any exception that occurs:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: [Ljava.lang.Object;
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.lang.ClassCastException: [Ljava.lang.Object;
com.BO.account.AccountActionBo.loadGroup(AccountActionBo.java:29)
com.action.account.AccountAction.loadGroup(AccountAction.java:54)
com.action.account.AccountAction.handleAction(AccountAction.java:42)
com.common.ChallengeAction.execute(ChallengeAction.java:39)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.



Name and version of the database you are using: mysql 5


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 7:02 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

What is the query that you passed.


Amila


Top
 Profile  
 
 Post subject: query passed to the hibernate
PostPosted: Tue May 22, 2007 7:07 am 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
This is the query i have passed to the hibernate

Pls help me .


"select accgrpid,accgrpdesc from account"


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 8:00 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi



query = "select * from accountgroup"

SQLQuery query=session.createSQLQuery(query).addEntity(Account.class);
List list=query.list();
if(list.size()>0)
account=(Account) list.get(0);

You are trying to cast a List object to Account type . that is cause to ClassCastException


Amila

(Don't forget to rate)


Top
 Profile  
 
 Post subject: sql grammer exception
PostPosted: Tue May 22, 2007 8:41 am 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
i am getting the sql grammer exception now

org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.loader.Loader.doList(Loader.java:2148)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
org.hibernate.loader.Loader.list(Loader.java:2024)
org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:111)
org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1655)
org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
com.DAO.ChallengeDAO.find(ChallengeDAO.java:34)
com.DAO.account.Accountgroup.loadGroup(Accountgroup.java:45)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 8:50 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi


You must select * fields from table. otherwise you should mention it as null

like this

    "select accgrpid,accgrpdesc, null as standardfrom accountgroup"



Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject: still having some exception
PostPosted: Tue May 22, 2007 9:26 am 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
still i am having this exception
i dont know y
then tell me where i need to add credit for u

org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.DAO.account.Accountgroup.setStandard
org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:215)
org.hibernate.tuple.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:185)
org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3232)
org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
org.hibernate.loader.Loader.doQuery(Loader.java:717)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
org.hibernate.loader.Loader.doList(Loader.java:2145)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
org.hibernate.loader.Loader.list(Loader.java:2024)
org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:111)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 9:50 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Can tou send code of setStandard() method

Amila


Top
 Profile  
 
 Post subject: here is the code
PostPosted: Tue May 22, 2007 9:53 am 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
i am getting the same error

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.DAO.account.Accountgroup" table="accountgroup" catalog="pao">
<id name="accgrpid" type="java.lang.Integer">
<column name="accgrpid" />
</id>
<property name="accgrpdesc" type="java.lang.String">
<column name="accgrpdesc" length="30" not-null="true" />
</property>
<property name="standard" type="java.lang.String">
<column name="standard" length="30" not-null="true" />
</property>
</class>
</hibernate-mapping>


public String getAccgrpid() {
return accgrpid;
}

public void setAccgrpid(String accgrpid) {
this.accgrpid = accgrpid;
}

public String getAccgrpdesc() {
return accgrpdesc;
}

public void setAccgrpdesc(String accgrpdesc) {
this.accgrpdesc = accgrpdesc;
}

public String getStandard() {
return standard;
}

public void setStandard(String standard) {
this.standard = standard;
}


Top
 Profile  
 
 Post subject: finally i have solved that
PostPosted: Tue May 22, 2007 10:01 am 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
thank you amaila finally i have solved all the error

thank you very much amaila


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 10:04 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

error is:
<property name="standard" type="java.lang.String">
<column name="standard" length="30" not-null="true" />
</property>

when we select null as standard . hibernate try to set null into statdard property. but it is not allowed. so you can do two things.

1. change not-null="false"
2. change query to "select * from accountgroup"

Amila
(Don't forget to rate)


Top
 Profile  
 
 Post subject: will u pls explain
PostPosted: Tue May 22, 2007 10:09 am 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
amila will u pls explain me whats happening while specifying

(session.createSQLQuery(query).addEntity(Accountgroup.class)).list();

addEntity of the particular class name whats happening there

is there any other possibility available without specifying that addEntity.

directly type casting the object.

pls help me


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 10:18 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

when using session.createSQLQuery(query) it will return a java.lang.Object array. we can't cast it to our mapped object. using addEntity() hibernate will fill a mapped object with appropriate value. so we can use that mapped object other than using a java.lang.Object array.


Amila


Top
 Profile  
 
 Post subject: what to do if i use createQuery
PostPosted: Tue May 22, 2007 10:19 am 
Beginner
Beginner

Joined: Mon Feb 05, 2007 2:33 am
Posts: 32
suppose if i am using

(session.createQuery(query)).list

then what should be done

now also i am getting the class cast exception is there any thing available to solve.

if i use createquery only it will be helpful for me because it automatically generates the hql.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 10:30 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Query query=session.createQuery(query);
List list=query.list();
if(list.size()>0)
account=(Account) list.get(0);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.