supplying a collection as a parameter to a NamedQuery

Hello,
We're using JPA and I'm trying to find an example of a NamedQuery that accepts a collection, e.g:
"select * from INVOICE where invoice_type in :types"

so when calling the Query I'd expect to pass in query.setParameter("types", myTypes);

where 'myTypes' is a List<InvoiceType>

this doesnt seem to work as outlined here; can someone tell me how to do this?
LVL 5
blackfrancis75Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ajay-SinghCommented:
Use Criteria,
 
session.createCriteria(Invoice.class).add(Restrictions.like("invoiceType",
myTypes))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jinesh KamdarCommented:
I'm not sure if the IN operator works like that. If the list is not too long, u can try looping thru' the list collection and providing the list value for the loop index to the SELECT.
0
ramiseesniblicCommented:
Are your types a list of String?

If they are, you can convert the collection of myTypes to a commea seperated list, and  passing in the commea seperated sting.
public static void main(String[] args ){
  //Code for getting connection and query
 
  String SQL = "select * from INVOICE where invoice_type in (?)"
  query.setParameter(1, listToString(myTypes) );
}
 
private String listToString( List<InvoceType> list ) {
  String str = ""
  if ( list.size() <= 0 ) {
    str += "-1";
  } else {
    for ( int i = 0; i < list.size(); i++ ) {
      InvoceType type = list.get(i);		    	
      str += //Code to get the string value of type from the object		        
      if ( i + 1 != list.size() ) {
        str += ",";
      }
    }
  }
 return str;
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.