You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by harry <su...@gmail.com> on 2012/01/09 09:34:25 UTC

hibernate type cast issue

Hi everyone.  I searched the archive and didn't find a thread on point.  I
encountered a hibernate type cast issue:

Variation 1:

List<Message> messageList = session.createSQLQuery("select * from Message
where id=1").list();
*message = messageList.get(0);*

The bold-faced line of code throws a "java.lang.ClassCastException:
[Ljava.lang.Object;"

Variation 2:

int messageid = 1;
Criteria crit = session.createCriteria(Message.class);
SimpleExpression se = Restrictions.eq("id", new Integer(messageid));
crit.add(se);
*List<Message> messageList = crit.list();*

The bold-faced line of code throws a "java.lang.ClassCastException:
java.lang.Integer."

Variation 3:

Criteria crit = session.createCriteria(Message.class);
List<Message> messageList = crit.list();
Message currentMessage = messageList.get(0);

No exception is thrown.

I spent hours trying to resolve this issue and am now thoroughly frustrated. 
Any help will be highly appreciated.

Harry


--
View this message in context: http://tapestry.1045711.n5.nabble.com/hibernate-type-cast-issue-tp5130777p5130777.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: hibernate type cast issue

Posted by Taha Hafeez Siddiqi <ta...@gmail.com>.
Hi Harry

It seems to me that you have posted in the wrong mailing list. :) Still I will try to answer this 

On Jan 9, 2012, at 2:04 PM, harry wrote:

> Hi everyone.  I searched the archive and didn't find a thread on point.  I
> encountered a hibernate type cast issue:
> 
> Variation 1:
> 
> List<Message> messageList = session.createSQLQuery("select * from Message
> where id=1").list();
> *message = messageList.get(0);*

It is because when you use Session#createSQLQuery(), it returns a list of Object[]. Each element in list in a row and each element in Object[] is a column :)
Try session.get(Message.class, 1) or session.createQuery("from Message where id=1").uniqueResult()

> 
> The bold-faced line of code throws a "java.lang.ClassCastException:
> [Ljava.lang.Object;"
> 
> Variation 2:
> 
> int messageid = 1;
> Criteria crit = session.createCriteria(Message.class);
> SimpleExpression se = Restrictions.eq("id", new Integer(messageid));
> crit.add(se);
> *List<Message> messageList = crit.list();*
> 
> The bold-faced line of code throws a "java.lang.ClassCastException:
> java.lang.Integer."
> 

It depends on what Message#id type is

> Variation 3:
> 
> Criteria crit = session.createCriteria(Message.class);
> List<Message> messageList = crit.list();
> Message currentMessage = messageList.get(0);
> 
> No exception is thrown.
> 
> I spent hours trying to resolve this issue and am now thoroughly frustrated. 
> Any help will be highly appreciated.
> 
> Harry
> 
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/hibernate-type-cast-issue-tp5130777p5130777.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 

regards
Taha


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org