user1048661
user1048661

Reputation: 51

Hibernate HQL list attributes?

I have a small query something like this:

String sqlStr = "select count(*) from MyTable n where primaryKey.userId = :userId and primaryKey.userType = :userType"

String[] paramNames = {"userId", "userType" };
Object[] paramValues = {userId, userType};

List myObjects = (List) hibernateTemplate.findByNamedQueryAndNamedParam("objectsToDeleteForUser", paramNames, paramValues);

and this works fine. I can make a call with a userid and usertype and get one or more matching records.

What I'd like to do now that is have a list of userid's and usertype's and perform the call but not have to iterate over the list but make one call. So basically when userid(0) and usertype(0), userid(1) and usertype(1) ...................

Is it possible to do this with one call in HQL. Something like the IN clause (X).

Thanks

Upvotes: 0

Views: 1253

Answers (1)

BaN3
BaN3

Reputation: 435

somwthing like this is possible :

hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
hibernateSession.beginTransaction();

List<Object> userId = new ArrayList<Object>();
List<Object> userType= new ArrayList<Object>();

Query q1 = hibernateSession.createQuery("select count(*) from MyTable n where primaryKey.userId IN (:userId) and primaryKey.userType IN (:userType)");

q1.setParameterList("userId", userId);
q1.setparameterList("userType" , userType);

List myObjects  = q1.list();

hibernateSession.close();

this should probably work for you

Upvotes: 0

Related Questions