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

list in HQL

 
Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • 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
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
the complete query goes like this.....
 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic