[jboss-user] [EJB 3.0] - Re: Using SQL IN

laszlo.fogas do-not-reply at jboss.com
Sat Oct 21 10:35:05 EDT 2006


"monkeyden" wrote : What is the proper way to do a query using SQL "IN"?  The only reference I found is this: http://www.jofti.com/guide/ch3-1.html#title_3_1_8, which is complaining about the colon for the parameter.
  | 
  | unexpected token: : near line 1, column 54 [from com.xxxxx.entity.Listing where id IN :ids]
  | 
  | id is the primary key of Listing.  My code is as follows:
  | 
  | @Entity
  |   | @Name("listing")
  |   |  @Table(name = "LISTING")
  |   | public class Listing implements java.io.Serializable {
  |   | 
  |   |     @Id
  |   |     @Column(name = "LISTING_ID", length = 22, nullable = false)
  |   |     public Long getId() {
  |   |         return this.id;
  |   |     }
  |   |     ...
  |   | }
  | 
  | 
  | 
  |   | List<Long> glids = new ArrayList<Long>();
  |   | 
  |   | String queryStr = "from Listing where id IN :ids";
  |   | Query query = em.createQuery(queryStr);
  |   | query.setParameter("ids", glids);
  |   | Object result = query.getResultList();

are you sure, that you can pass a list to the query?

I don't if it handles the list or not, but i think you can generate the query string dinamically:

String queryStr = "from Listing where id IN (";
for(Long l: glids){
queryStr += l.toString+", ";
}
queryStr += ") ";

should work..


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3979799#3979799

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3979799



More information about the jboss-user mailing list