• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

list in HQL

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have a doubt in hibernate

can I pass a list of string values to the where condition of HQL query?



list contains string of usernames
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes. You can.
You need to have it like this,

select user.userid where user_name in (:list);

and later replace it in the Hiberante Session with the actual java.lang.List. If you are using a List of Strings, then it would work. If you are using a user defined Object, then probably you need to override the toString() method to return the appropriate string.
 
Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks arun
[ August 06, 2007: Message edited by: Madhusudhana E ]
 
Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the complete query goes like this.....
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have this similar problem but the list that i need to pass is a collection of Objects(User-Defined) What exactly I have to do make this run.

This is my code.

private List<Coverage> getListOfCompanies(
List<PartyRelationship> listOfAnalyst)
throws TiNAException {

Coverage pojo = null;
HibernateConnection hc = null;

StringBuilder sqllist = new StringBuilder();

for(PartyRelationship pojoObj : listOfAnalyst)
sqllist.append(pojoObj.getId().getRelPartyId() + ",");

sqllist.delete(sqllist.length()-1, sqllist.length());


try {
hc = ConnectionManager.getConnection(ConnectionManager.QUANTUM);
} catch (ConnectionNotFoundException ex) {
throw new TiNAException("Could not get connection: " +
ex.getMessage());
}

Session session = hc.getSession();
Transaction tx = session.beginTransaction();

String query = "from " + Coverage.class.getCanonicalName() +
" c where c.id in(:sqllist)";

List<Coverage> listofCompanyIds = session.createQuery(query).setParameter("sqllist", sqllist.toString()).list();

if (!listofCompanyIds.isEmpty() || (listofCompanyIds != null)) {
tx.commit();
session.close();

return listofCompanyIds;
} else {
throw new TiNAException(
"Could not find PartyRelationShip instance!");
}
}


I am actually trying to get generate a list of paramters and seperate it wid comma so that it can be passed to the sql statement in the IN clause.

But i belive this is wrong.

How can i directly pass the listOfAnalyst -collection object directly in the query?Also let me know how to i run this on HQL editor .

When i run this on HQL editor
from Coverage c where c.id in( 100000007, 100000199, 100000704, 100001101, 100002169,
100002209, 100002298, 100002401, 100002427, 100002674, 100002691,100002806,100003009, 100651750
)

it says com.sybase.jdbc2.jdbc.SybSQLException: Incorrect syntax near ','.


[ August 20, 2007: Message edited by: Nilesh Raje ]
[ August 20, 2007: Message edited by: Nilesh Raje ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic