-->
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.  [ 2 posts ] 
Author Message
 Post subject: ClassCastException when from session.load?
PostPosted: Mon May 22, 2006 10:35 pm 
Newbie

Joined: Mon Nov 03, 2003 1:07 am
Posts: 14
When I fetch by an object by name using an hql query, I can cast it. If I fetch it by ID, if I cast it, I get a ClassCastException.

There is something strange that I don't understand about doing a query vs. doing a session.load.

I would appreciate a good slapping on what docs I didn't read. Googling for hours hasn't helped.


Hibernate version:
3.2.0.cr1

Mapping documents:
Using annotations

Code between sessionFactory.openSession() and session.close():
public Content findContentByName(final String cname) {
Content content = (Content) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session
.createQuery("select c from Content c where c.name = :name");
query.setString("name", cname);

Content content = (Content) query.uniqueResult();
init(content);
return content;
}
});

return content;
}
public Content findContentById(final Long contentId) {
log.debug("findContentById," + contentId);
Content content = (Content) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Content content = (Content) session.load(Content.class,
contentId.longValue());

init(content);
return content;
}
});
return content;
}
Full stack trace of any exception that occurs:
Exception in thread "main" java.lang.ClassCastException: org.authsum.stitches.om.Content$$EnhancerByCGLIB$$239f00a5
at org.authsum.stitches.example.Test.main(Test.java:18)
Name and version of the database you are using:
Mysql 4.x
The generated SQL (show_sql=true):
This sql is generated when I do a name search. No sql is generated from the session.load call (may be cached)
select
content0_.object_id as object2_0_,
content0_.version as version0_,
content0_.create_date as create4_0_,
content0_.name as name0_,
content0_.description as descript6_0_,
content0_.stitch_object_id as stitch14_0_,
content0_.absolutePath as absolute7_0_,
content0_.displayName as displayN8_0_,
content0_.secureToken as secureTo9_0_,
content0_.absoluteUrl as absolut10_0_,
content0_.url as url0_,
content0_.smallThumbnailImage_object_id as smallTh15_0_,
content0_.mediumThumbnailImage_object_id as mediumT16_0_,
content0_.largeThumbnailImage_object_id as largeTh17_0_,
content0_.height as height0_,
content0_.width as width0_,
content0_.DTYPE as DTYPE0_
from
Content content0_
where
content0_.name=?
Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 4:07 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 5:33 am
Posts: 31
As I've understood it, if you specify "select" in your query you will only get the specified columns, which are not instances of the "Content" class but Strings, longs etc.

If you change your query to:

session.createQuery("from Content c where c.name = :name");
query.setString("name", cname);

It should load Objects of class Content, and no ClassCastException should occur.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.