Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA trying to pass list to IN part of query

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying the following with no luck how would I go about passing a list as a parameter and then using it

List<Number> list =
em.createQuery("select number from Number number where number.phone in (:numberList)")
.setParameter("numberList", thisIsAList<Number> .getResultList();

When I change the "where number.phone" portion of the query to "where number" I get a object references an unsaved transient instance - save the transient instance before flushing: Number error

Thank you all for your time
[ July 16, 2008: Message edited by: john mattucci ]
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shouldn't we be using .setParameterList as opposed to .setParameter.
 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such method in javax.persistence.Query. If I change the list to contain Strings and the leave the "where number.phone" portion as is it works. What I'm trying to do is check if the numbers already exist in the DB. My question now is can I attempt what I'm trying to do i.e. use the Number object to see if the data is the DB or is it trying to tell me that I cannot do that unless that data is first present in the DB?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by john mattucci:
There is no such method in javax.persistence.Query. If I change the list to contain Strings and the leave the "where number.phone" portion as is it works. What I'm trying to do is check if the numbers already exist in the DB. My question now is can I attempt what I'm trying to do i.e. use the Number object to see if the data is the DB or is it trying to tell me that I cannot do that unless that data is first present in the DB?


I guess you can think of it this way, if you just use Number in the Where, how is that going to be translated to an SQL query to pass to the database. Include all the fields of the object in the where? Does sound very effective.

In Hibernate, not JPA there is the Criteria object, where you can use an Object with some data filled as an example to create a where clause with just the fields that have values.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic