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.  [ 3 posts ] 
Author Message
 Post subject: help with createSQLQuery, getting ClassCastException
PostPosted: Wed May 06, 2009 2:48 pm 
Newbie

Joined: Wed Apr 08, 2009 1:11 pm
Posts: 19
Hi,

I have 2 entities with a join condition on column BOOK_ID. One is Chapter, which has its own BOOK_ID column and one is Book whose getId() column is also BOOK_ID. Note that I have not done a many-to-one mapping on Chapter.BOOK_ID to let Hibernate know about the possible join.

I'm trying to use createSQLQuery:

select c.CHAPTER_ID, (b.*)
from CHAPTERS c
join BOOKS b
on b.BOOK_ID = c.BOOK_ID
where c.CHAPTER_ID in (:idlist)

and my java snippet to create this query with bindings is:

SQLQuery q = createSQLQuery(above_snippet);
q.addEntity("c", Chapter.class);
q.addEntity("b", Book.class);
q.addJoin("b", "c.bookId");
q.setParameterList("idlist", idarray, new LongType());

then execute and get ClassCastException:

Exception in thread "main" java.lang.ClassCastException: org.hibernate.type.LongType
at org.hibernate.loader.custom.CustomLoader.<init>(CustomLoader.java:174)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1715)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:175)

Am I doing something wrong, or is it not possible to use SQLQuery with join unless you have a Hibernate many-to-one mapping on the join column.

Andy


Top
 Profile  
 
 Post subject: Re: help with createSQLQuery, getting ClassCastException
PostPosted: Wed May 06, 2009 4:19 pm 
Regular
Regular

Joined: Mon Apr 19, 2004 6:54 pm
Posts: 79
Have you tried with:
Code:
q.setParameterList("idlist", Hibernate.LONG);


Christophe


Top
 Profile  
 
 Post subject: Re: help with createSQLQuery, getting ClassCastException
PostPosted: Wed May 06, 2009 8:19 pm 
Newbie

Joined: Wed Apr 08, 2009 1:11 pm
Posts: 19
Yes, I tried this, with the same result. It appears that hibernate parses the native SQL query to marshall objects, and it only allows me to do table joins on business objects that I've given it many-to-one relationships on. I'm really disappointed.


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