smali
smali

Reputation: 4805

Can we create a HQL query with same name but different Parameters?

Can we create a HQL query with same name but different Parameters,

and call it using findByNamedQueryAndNamedParam() method.

But the hibernate implementation of this metod is as follows.

public List findByNamedQueryAndNamedParam(
        final String queryName, final String[] paramNames, final Object[] values)
        throws DataAccessException {

    if (paramNames != null && values != null && paramNames.length != values.length) {
        throw new IllegalArgumentException("Length of paramNames array must match length of values array");
    }
    return executeWithNativeSession(new HibernateCallback<List>() {
        public List doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.getNamedQuery(queryName);
            prepareQuery(queryObject);
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
                }
            }
            return queryObject.list();
        }
    });
}

So is it possible to create a HQL Query with same name but different parameters and call the query on the basis of Parameters dynamically?

Upvotes: 0

Views: 721

Answers (1)

Viraj Nalawade
Viraj Nalawade

Reputation: 3225

As far as I know you cannot have multiple named queries with same name. You can write a dynamic query for this. Check this answer to see how to do this in HQL. Its showing how the Criteria API makes it simpler and is sounding better for your scenario.
Also this answer shows a unconventional way of doing something similar to your query.

Upvotes: 1

Related Questions