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:
|