How to pass the more than 1000 parameters to
the sql in query
// It should not changed. Criteria allows
1000 records for the "in" method.
private static final Integer SUPPORTED_IN_PARAMETER_ARGUMENTS = 1000;
private static final String SPACE = "
";
private static final String AND = "
and ";
private static final String OR = " or
";
private static final String OPEN_BRACE = "
( ";
private static final String CLOSE_BRACE = "
) ";
private static final String IN = " in
";
Criteria
public static final void addParametersToCriteriaInMethod(Criteria
criteria, Collection<?> collection, String property)
{
if (criteria != null &&
collection != null && property != null)
{
// new Array List object requires for
operations, "Arrays.asList' dosen't give the new list it gives
// the java.util.Arrays$ArrayList, on
this object we can't do any operations
List<Object> listOfParameters = new ArrayList<Object>(Arrays.asList(collection.toArray()));
Disjunction orCondiation = Restrictions.disjunction();
if (listOfParameters.size() >
SUPPORTED_IN_PARAMETER_ARGUMENTS)
{
while (listOfParameters.size() > SUPPORTED_IN_PARAMETER_ARGUMENTS)
{
orCondiation.add(Restrictions.in(property, listOfParameters.subList(0,
SUPPORTED_IN_PARAMETER_ARGUMENTS)));
listOfParameters.subList(0, SUPPORTED_IN_PARAMETER_ARGUMENTS).clear();
}
}
orCondiation.add(Restrictions.in(property, listOfParameters));
criteria.add(orCondiation);
}
else
{
throw new IllegalArgumentException("One of the passed parameters was
null");
}
}
Detached Criteria
public static final void
addParametersToCriteriaInMethod(DetachedCriteria criteria, Collection<?>
collection, String property)
{
if (criteria != null &&
collection != null && property != null)
{
// new Array List object requires for
operations, "Arrays.asList' dosen't give the new list it gives
// the java.util.Arrays$ArrayList, on
this object we can't do any operations
List<Object> listOfParameters = new ArrayList<Object>(Arrays.asList(collection.toArray()));
Disjunction orCondiation = Restrictions.disjunction();
if (listOfParameters.size() >
SUPPORTED_IN_PARAMETER_ARGUMENTS)
{
while (listOfParameters.size() >
SUPPORTED_IN_PARAMETER_ARGUMENTS)
{
orCondiation.add(Restrictions.in(property, listOfParameters.subList(0,
SUPPORTED_IN_PARAMETER_ARGUMENTS)));
listOfParameters.subList(0, SUPPORTED_IN_PARAMETER_ARGUMENTS).clear();
}
}
orCondiation.add(Restrictions.in(property, listOfParameters));
criteria.add(orCondiation);
}
else
{
throw new IllegalArgumentException("One of the passed parameters was
null");
}
}
HQL
public static final String addParametersToHQLInMethod(String query, Collection<?>
collection, String property, Boolean addAnd)
{
StringBuilder sb = new StringBuilder(query);
List<Object> l = new
ArrayList<Object>(Arrays.asList(collection.toArray()));
if (addAnd)
{
sb.append(AND);
}
sb.append(OPEN_BRACE);
if (l.size() > SUPPORTED_IN_PARAMETER_ARGUMENTS)
{
while (l.size() > SUPPORTED_IN_PARAMETER_ARGUMENTS)
{
sb.append(SPACE + property + IN + l.subList(0,
SUPPORTED_IN_PARAMETER_ARGUMENTS) + OR);
l.subList(0, SUPPORTED_IN_PARAMETER_ARGUMENTS).clear();
}
}
sb.append(SPACE + property + IN + l + CLOSE_BRACE);
return sb.toString().replace("[", OPEN_BRACE).replace("]", CLOSE_BRACE);
}
No comments:
Post a Comment