-->
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: CollectionOfElements and IN clause
PostPosted: Fri Jun 22, 2007 11:24 am 
Newbie

Joined: Fri Jun 22, 2007 10:52 am
Posts: 2
Hello,

I have User entity class with this mapping:

Code:
@CollectionOfElements
@JoinTable(name = "User_roles", joinColumns = @JoinColumn(name = "User_id"))
private Set<String> roles = new HashSet<String>();


The generated table looks like this:
Code:
CREATE TABLE `User_roles` (
  `User_id` bigint(20) NOT NULL,
  `element` varchar(255) default NULL,
  KEY `FKEA147569555F3082` (`User_id`)
)


I want to select all Users having one or more specific roles. For that, I defined a NamedQuery like this:

Code:
@NamedQuery(
   name = "User.findByRole",
   query = "SELECT user FROM User user WHERE user.roles IN (:roles)"
)


I then have this method in my service bean:

Code:
public List<User> findByRole(String ... roles) {
   Query q = getEntityManager().createNamedQuery("User.findByRole");
   q.setParameter("roles", roles);
   return q.getResultList();
}


I then get the following error message from Mysql driver:

JDBCExceptionReporter.logExceptions - SQL Error: 0, SQLState: 07001
JDBCExceptionReporter.logExceptions - No value specified for parameter 1

If you take a look below, the generated query is missing even the reference to the element column.

I have the Persistence with Hibernate book, but I just don't see what I'm doing wrong here.

Can anybody help me here? It seems that what I want to accomplish is rather straight forward, isn't it?

Hibernate version:
Hibernate 3.2.4.sp1, EM 3.3.1, Annot 3.3.0

Name and version of the database you are using:
MySQL 5.0.38-Ubuntu_0ubuntu1-log

The generated SQL (show_sql=true):
Code:
    select
        user0_.id as id7_,
        user0_.email_id as email10_7_,
        user0_.login as login7_,
        user0_.password as password7_,
    from
        User user0_,
        User_roles roles1_
    where
        user0_.id=roles1_.User_id
        and (
            . in (
                ?
            )
        )


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 8:46 am 
Newbie

Joined: Fri Jun 22, 2007 10:52 am
Posts: 2
This thread on Jboss.com helped me find a solution:
http://www.jboss.com/index.html?module= ... 29#3921329

The Query must be defined like this:
Code:
@NamedQuery(
   name = "User.findByRole",
   query = "SELECT user FROM User user JOIN user.roles ur WHERE ur IN (:roles)"
)


Then, I also had to switch to Hibernate API to do the Query:

Code:
org.hibernate.Query hq = getHibernateSession().getNamedQuery("User.findByRole");
hq.setParameterList("roles", roles);
return hq.list();


Using the Query.setParameter() with a String array didn't work, as Hibernate wanted to convert it to a single String (resulting in ClassCastException).

Documented here in case someone else needs it.


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.