This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help:Passing List of User-defined Objects in the IN clause of HQL  RSS feed

Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a 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: " +

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

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

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

if (!listofCompanyIds.isEmpty() || (listofCompanyIds != null)) {

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 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 ','.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!