Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.hibernate.operations;
import io.micronaut.aop.InvocationContext;
import io.micronaut.context.ApplicationContext;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.data.annotation.QueryHint;
import io.micronaut.data.jpa.annotation.EntityGraph;
import io.micronaut.data.model.Pageable;
import io.micronaut.data.model.Pageable.Mode;
import io.micronaut.data.model.Sort;
import io.micronaut.data.model.query.builder.jpa.JpaQueryBuilder;
import io.micronaut.data.model.runtime.PagedQuery;
import io.micronaut.data.model.runtime.PreparedQuery;
import io.micronaut.data.model.runtime.QueryParameterBinding;
import io.micronaut.data.model.runtime.RuntimeEntityRegistry;
import io.micronaut.data.model.runtime.RuntimePersistentEntity;
import io.micronaut.data.model.runtime.RuntimePersistentProperty;
import io.micronaut.data.model.runtime.StoredQuery;
import io.micronaut.data.operations.HintsCapableRepository;
import io.micronaut.data.runtime.convert.DataConversionService;
import io.micronaut.data.runtime.mapper.BeanIntrospectionMapper;
import io.micronaut.data.runtime.operations.internal.query.BindableParametersPreparedQuery;
import io.micronaut.data.runtime.operations.internal.query.BindableParametersStoredQuery;
import io.micronaut.data.runtime.operations.internal.query.DefaultBindableParametersPreparedQuery;
import io.micronaut.data.runtime.operations.internal.query.DefaultBindableParametersStoredQuery;
import io.micronaut.data.runtime.query.PreparedQueryDecorator;
import io.micronaut.data.runtime.query.StoredQueryDecorator;
import org.hibernate.graph.AttributeNode;
import org.hibernate.graph.Graph;
import org.hibernate.graph.RootGraph;
import org.hibernate.graph.SubGraph;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.Tuple;
import jakarta.persistence.TupleElement;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Order;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Abstract Hibernate operations shared between the synchronous and the reactive implementations.
*
* @param The session type
* @param The query type
* @param
The selection query type
* @author Denis Stepanov
* @since 3.5.0
*/
@Internal
public abstract class AbstractHibernateOperations implements HintsCapableRepository, PreparedQueryDecorator, StoredQueryDecorator {
private static final JpaQueryBuilder QUERY_BUILDER = new JpaQueryBuilder();
private static final String ENTITY_GRAPH_FETCH = "jakarta.persistence.fetchgraph";
private static final String ENTITY_GRAPH_LOAD = "jakarta.persistence.loadgraph";
protected final ConversionService dataConversionService;
protected final RuntimeEntityRegistry runtimeEntityRegistry;
/**
* Default constructor.
*
* @param runtimeEntityRegistry The runtime entity registry
* @param dataConversionService The data conversion service
*/
protected AbstractHibernateOperations(RuntimeEntityRegistry runtimeEntityRegistry, DataConversionService dataConversionService) {
this.runtimeEntityRegistry = runtimeEntityRegistry;
// Backwards compatibility should be removed in the next version
this.dataConversionService = dataConversionService == null ? ConversionService.SHARED : dataConversionService;
}
@Override
public PreparedQuery decorate(PreparedQuery preparedQuery) {
return new DefaultBindableParametersPreparedQuery<>(preparedQuery);
}
@Override
public StoredQuery decorate(StoredQuery storedQuery) {
RuntimePersistentEntity runtimePersistentEntity = runtimeEntityRegistry.getEntity(storedQuery.getRootEntity());
return new DefaultBindableParametersStoredQuery<>(storedQuery, runtimePersistentEntity);
}
/**
* @return The application context
*/
protected ApplicationContext getApplicationContext() {
return runtimeEntityRegistry.getApplicationContext();
}
/**
* @return The conversion service
*/
protected ConversionService getConversionService() {
return dataConversionService;
}
/**
* Gets the persistence entity.
*
* @param type The entity type
* @param The entity type
* @return The persistent entity
*/
@NonNull
protected abstract RuntimePersistentEntity getEntity(@NonNull Class type);
@Override
@NonNull
public Map getQueryHints(@NonNull StoredQuery, ?> storedQuery) {
AnnotationMetadata annotationMetadata = storedQuery.getAnnotationMetadata();
if (annotationMetadata.hasAnnotation(EntityGraph.class)) {
String hint = annotationMetadata.stringValue(EntityGraph.class, "hint").orElse(ENTITY_GRAPH_FETCH);
String graphName = annotationMetadata.stringValue(EntityGraph.class).orElse(null);
String[] paths = annotationMetadata.stringValues(EntityGraph.class, "attributePaths");
// If the graphName is set, it takes precedence over the attributeNames
if (graphName != null) {
return Collections.singletonMap(hint, graphName);
} else if (ArrayUtils.isNotEmpty(paths)) {
return Collections.singletonMap(hint, paths);
}
}
return Collections.emptyMap();
}
/**
* Sets a parameter into query.
*
* @param query The query
* @param parameterName The parameter name
* @param value The value
*/
protected abstract void setParameter(Q query, String parameterName, Object value);
/**
* Sets parameter into query.
*
* @param query The query
* @param parameterName The parameter name
* @param value The value
* @param argument The argument
*/
protected abstract void setParameter(Q query, String parameterName, Object value, Argument> argument);
/**
* Sets a list parameter into query.
*
* @param query The query
* @param parameterName The parameter name
* @param value The value
*/
protected abstract void setParameterList(Q query, String parameterName, Collection