org.bonitasoft.engine.persistence.HQLQueryBuilder Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2019 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.bonitasoft.engine.persistence;
import java.util.Collection;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.query.ParameterMetadata;
import org.hibernate.query.Query;
import org.hibernate.query.QueryParameter;
import org.hibernate.type.CustomType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Baptiste Mesta
*/
public class HQLQueryBuilder extends QueryBuilder {
private static final Logger logger = LoggerFactory.getLogger(HQLQueryBuilder.class);
HQLQueryBuilder(Session session, Query baseQuery, OrderByBuilder orderByBuilder,
Map classAliasMappings,
char likeEscapeCharacter,
OrderByCheckingMode orderByCheckingMode,
SelectListDescriptor selectDescriptor) {
super(session, baseQuery, orderByBuilder, classAliasMappings, likeEscapeCharacter,
orderByCheckingMode, selectDescriptor);
}
@Override
Query rebuildQuery(AbstractSelectDescriptor selectDescriptor, Session session, Query query) {
Query generatedQuery = session.createQuery(stringQueryBuilder.toString());
ParameterMetadata parameterMetadata = generatedQuery.getParameterMetadata();
for (Map.Entry parameter : getQueryParameters().entrySet()) {
if (parameter.getValue() instanceof Collection) {
generatedQuery.setParameterList(parameter.getKey(), (Collection) parameter.getValue());
} else {
generatedQuery.setParameter(parameter.getKey(),
convertParameterValueToFieldType(parameter, parameterMetadata));
}
}
return generatedQuery;
}
private Object convertParameterValueToFieldType(Map.Entry parameter,
ParameterMetadata parameterMetadata) {
QueryParameter