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.  [ 9 posts ] 
Author Message
 Post subject: JPA + Hibernate - Bug with Where "In" ?
PostPosted: Tue May 06, 2008 11:50 am 
Newbie

Joined: Tue May 06, 2008 11:45 am
Posts: 6
I have a simple select clause with an "IN" I set the parameter as a String Array but get thrown a runtime error saying Hibernate is expecting just a String. When I use an IN it's always because I want to match more than one item. Anyone else see this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 3:02 pm 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
Are you sure, that the property name is right?
Can you post your code? I am using Restrictions.in with an Collection (ArrayList<String>) and everything is working fine.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 4:00 pm 
Newbie

Joined: Tue May 06, 2008 11:45 am
Posts: 6
This code works by taking languages, which might be "English", "Spanish" and "Other" and puts the code in line:

Query q = em.createNativeQuery("select id from DT_Collection_Language d where d.language in ('Enlgish', 'Spanish', 'Other') ");


This code throws Exception at Runtime:

java.lang.ClassCastException: [Ljava.lang.String;
at org.hibernate.type.StringType.toString(StringType.java:44)


...
...
@NamedQuery(name="findtest", query="select a.id from DowntimeLanguage a where a.language in (?1)")
...

String[] languagesA = {"English", "Spanish", "Other"};
Query q = em.createNamedQuery("findtest");
q.setParameter(1, languagesA);
return q.getResultList();

The error is thrown at the line q.getResultList() and the query is never run (I have showSQL turned on)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 5:47 pm 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
I am not an hibernate expert, but I remember that I had problems by selecting only one column. You tried to select the whole object?

Every thought about Criteria Queries? Should be much easier!

_________________
##########################
Please don't forget to rate... Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 6:07 pm 
Newbie

Joined: Tue May 06, 2008 11:45 am
Posts: 6
I'm trying to stick with the JPA spec. Yes, this works with just Hibernate, but JPA with Hibernate it does not work. I believe it is a bug in Hibernate.


Top
 Profile  
 
 Post subject: question
PostPosted: Sun May 11, 2008 9:11 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Hi,
What do you get if you simply execute the JPA query:
"select id from DT_Collection_Language d"
?
Does it work?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 11:15 am 
Newbie

Joined: Tue May 06, 2008 11:45 am
Posts: 6
Yes it does work and returns all rows of the table


Top
 Profile  
 
 Post subject: List
PostPosted: Tue May 13, 2008 6:53 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Try using the List interface instead of a String[] array. e.g.
List languages = new ArrayList();
languages.add...
languages.add...
Finally, call setParameter(1, languages)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 12:11 pm 
Newbie

Joined: Tue May 06, 2008 11:45 am
Posts: 6
It Worked! THANK YOU!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.