use of org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata in project hibernate-orm by hibernate.
the class AbstractManipulationCriteriaQuery method interpret.
@Override
public CriteriaInterpretation interpret(RenderingContext renderingContext) {
final String jpaqlString = renderQuery(renderingContext);
return new CriteriaInterpretation() {
@Override
@SuppressWarnings("unchecked")
public QueryImplementor buildCompiledQuery(SessionImplementor entityManager, final InterpretedParameterMetadata interpretedParameterMetadata) {
final Map<String, Class> implicitParameterTypes = extractTypeMap(interpretedParameterMetadata.implicitParameterBindings());
QueryImplementor query = entityManager.createQuery(jpaqlString, null, null, new HibernateEntityManagerImplementor.QueryOptions() {
@Override
public List<ValueHandlerFactory.ValueHandler> getValueHandlers() {
return null;
}
@Override
public Map<String, Class> getNamedParameterExplicitTypes() {
return implicitParameterTypes;
}
@Override
public ResultMetadataValidator getResultMetadataValidator() {
return null;
}
});
for (ImplicitParameterBinding implicitParameterBinding : interpretedParameterMetadata.implicitParameterBindings()) {
implicitParameterBinding.bind(query);
}
return query;
}
private Map<String, Class> extractTypeMap(List<ImplicitParameterBinding> implicitParameterBindings) {
final HashMap<String, Class> map = new HashMap<>();
for (ImplicitParameterBinding implicitParameter : implicitParameterBindings) {
map.put(implicitParameter.getParameterName(), implicitParameter.getJavaType());
}
return map;
}
};
}
use of org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata in project hibernate-orm by hibernate.
the class CriteriaQueryImpl method interpret.
@Override
public CriteriaInterpretation interpret(RenderingContext renderingContext) {
final StringBuilder jpaqlBuffer = new StringBuilder();
queryStructure.render(jpaqlBuffer, renderingContext);
if (!getOrderList().isEmpty()) {
jpaqlBuffer.append(" order by ");
String sep = "";
for (Order orderSpec : getOrderList()) {
jpaqlBuffer.append(sep).append(((Renderable) orderSpec.getExpression()).render(renderingContext)).append(orderSpec.isAscending() ? " asc" : " desc");
sep = ", ";
}
}
final String jpaqlString = jpaqlBuffer.toString();
log.debugf("Rendered criteria query -> %s", jpaqlString);
return new CriteriaInterpretation() {
@Override
@SuppressWarnings("unchecked")
public QueryImplementor buildCompiledQuery(SessionImplementor entityManager, final InterpretedParameterMetadata parameterMetadata) {
final Map<String, Class> implicitParameterTypes = extractTypeMap(parameterMetadata.implicitParameterBindings());
QueryImplementor<T> jpaqlQuery = entityManager.createQuery(jpaqlString, getResultType(), getSelection(), new HibernateEntityManagerImplementor.QueryOptions() {
@Override
public List<ValueHandlerFactory.ValueHandler> getValueHandlers() {
SelectionImplementor selection = (SelectionImplementor) queryStructure.getSelection();
return selection == null ? null : selection.getValueHandlers();
}
@Override
public Map<String, Class> getNamedParameterExplicitTypes() {
return implicitParameterTypes;
}
@Override
public ResultMetadataValidator getResultMetadataValidator() {
return new HibernateEntityManagerImplementor.QueryOptions.ResultMetadataValidator() {
@Override
public void validate(Type[] returnTypes) {
SelectionImplementor selection = (SelectionImplementor) queryStructure.getSelection();
if (selection != null) {
if (selection.isCompoundSelection()) {
if (returnTypes.length != selection.getCompoundSelectionItems().size()) {
throw new IllegalStateException("Number of return values [" + returnTypes.length + "] did not match expected [" + selection.getCompoundSelectionItems().size() + "]");
}
} else {
if (returnTypes.length > 1) {
throw new IllegalStateException("Number of return values [" + returnTypes.length + "] did not match expected [1]");
}
}
}
}
};
}
});
for (ImplicitParameterBinding implicitParameterBinding : parameterMetadata.implicitParameterBindings()) {
implicitParameterBinding.bind(jpaqlQuery);
}
return new CriteriaQueryTypeQueryAdapter(entityManager, jpaqlQuery, parameterMetadata.explicitParameterInfoMap());
}
private Map<String, Class> extractTypeMap(List<ImplicitParameterBinding> implicitParameterBindings) {
final HashMap<String, Class> map = new HashMap<String, Class>();
for (ImplicitParameterBinding implicitParameter : implicitParameterBindings) {
map.put(implicitParameter.getParameterName(), implicitParameter.getJavaType());
}
return map;
}
};
}
Aggregations