io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityJoinSupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-data-model Show documentation
Show all versions of micronaut-data-model Show documentation
Data Repository Support for Micronaut
The newest version!
/*
* Copyright 2017-2021 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.model.jpa.criteria.impl;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.data.model.Association;
import io.micronaut.data.model.PersistentEntity;
import io.micronaut.data.model.PersistentProperty;
import io.micronaut.data.model.jpa.criteria.PersistentAssociationPath;
import io.micronaut.data.model.jpa.criteria.PersistentCollectionAssociationPath;
import io.micronaut.data.model.jpa.criteria.PersistentEntityCollectionJoin;
import io.micronaut.data.model.jpa.criteria.PersistentEntityFrom;
import io.micronaut.data.model.jpa.criteria.PersistentEntityJoin;
import io.micronaut.data.model.jpa.criteria.PersistentEntityListJoin;
import io.micronaut.data.model.jpa.criteria.PersistentEntitySetJoin;
import io.micronaut.data.model.jpa.criteria.PersistentListAssociationPath;
import io.micronaut.data.model.jpa.criteria.PersistentSetAssociationPath;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Fetch;
import jakarta.persistence.criteria.From;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.MapJoin;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.metamodel.CollectionAttribute;
import jakarta.persistence.metamodel.ListAttribute;
import jakarta.persistence.metamodel.MapAttribute;
import jakarta.persistence.metamodel.PluralAttribute;
import jakarta.persistence.metamodel.SetAttribute;
import jakarta.persistence.metamodel.SingularAttribute;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import static io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.notSupportedOperation;
/**
* The abstract implementation of {@link PersistentEntityFrom}.
*
* @param The associated entity owner type
* @param The association entity type
* @author Denis Stepanov
* @since 3.2
*/
@Internal
public abstract class AbstractPersistentEntityJoinSupport implements PersistentEntityFrom {
protected final Map> joins = new LinkedHashMap<>();
@Override
public abstract PersistentEntity getPersistentEntity();
protected abstract PersistentAssociationPath createJoinAssociation(@NonNull Association association,
@Nullable io.micronaut.data.annotation.Join.Type type,
@Nullable String alias);
private PersistentAssociationPath getJoin(String attributeName) {
return getJoin(attributeName, null, null);
}
private PersistentAssociationPath getJoin(String attributeName, io.micronaut.data.annotation.Join.Type type) {
return getJoin(attributeName, type, null);
}
private PersistentAssociationPath getJoin(String attributeName, io.micronaut.data.annotation.Join.Type type, String alias) {
PersistentProperty persistentProperty = getPersistentEntity().getPropertyByName(attributeName);
if (!(persistentProperty instanceof Association association)) {
throw new IllegalStateException("Expected an association for attribute name: " + attributeName);
}
PersistentAssociationPath path = joins.computeIfAbsent(attributeName, a -> createJoinAssociation(association, type, alias));
if (type != null && type != io.micronaut.data.annotation.Join.Type.DEFAULT) {
path.setAssociationJoinType(type);
}
if (alias != null) {
path.setAlias(alias);
}
return (PersistentAssociationPath) path;
}
private PersistentCollectionAssociationPath getCollectionJoin(String attributeName, io.micronaut.data.annotation.Join.Type type) {
PersistentAssociationPath join = getJoin(attributeName, type);
if (!(join instanceof PersistentCollectionAssociationPath persistentCollectionAssociationPath)) {
throw new IllegalStateException("Join is not a Collection!");
}
return persistentCollectionAssociationPath;
}
private PersistentSetAssociationPath getSetJoin(String attributeName, io.micronaut.data.annotation.Join.Type type) {
PersistentAssociationPath join = getJoin(attributeName, type);
if (!(join instanceof PersistentSetAssociationPath persistentSetAssociationPath)) {
throw new IllegalStateException("Join is not a Set!");
}
return persistentSetAssociationPath;
}
private PersistentListAssociationPath getListJoin(String attributeName, io.micronaut.data.annotation.Join.Type type) {
PersistentAssociationPath join = getJoin(attributeName, type);
if (!(join instanceof PersistentListAssociationPath persistentListAssociationPath)) {
throw new IllegalStateException("Join is not a List!");
}
return persistentListAssociationPath;
}
@Override
public > Expression get(MapAttribute map) {
return get(map.getName());
}
@Override
public > Expression get(PluralAttribute collection) {
return get(collection.getName());
}
@Override
public PersistentEntityJoin join(String attributeName) {
return getJoin(attributeName);
}
@Override
public PersistentEntityJoin join(String attributeName, JoinType jt) {
return getJoin(attributeName, convert(Objects.requireNonNull(jt)));
}
@Override
public PersistentEntityJoin join(String attributeName, io.micronaut.data.annotation.Join.Type type) {
return getJoin(attributeName, Objects.requireNonNull(type));
}
@Override
public PersistentEntityJoin join(String attributeName, io.micronaut.data.annotation.Join.Type type, String alias) {
return getJoin(attributeName, Objects.requireNonNull(type), Objects.requireNonNull(alias));
}
@Nullable
private io.micronaut.data.annotation.Join.Type convert(@Nullable JoinType joinType) {
if (joinType == null) {
return null;
}
return switch (joinType) {
case LEFT -> io.micronaut.data.annotation.Join.Type.LEFT_FETCH;
case RIGHT -> io.micronaut.data.annotation.Join.Type.RIGHT_FETCH;
case INNER -> io.micronaut.data.annotation.Join.Type.INNER;
};
}
@Override
public PersistentEntityJoin join(SingularAttribute super E, Y> attribute) {
return getJoin(attribute.getName());
}
@Override
public PersistentEntityJoin join(SingularAttribute super E, Y> attribute, JoinType jt) {
return getJoin(attribute.getName(), convert(Objects.requireNonNull(jt)));
}
@Override
public PersistentEntityCollectionJoin join(CollectionAttribute super E, Y> collection, JoinType jt) {
return getCollectionJoin(collection.getName(), convert(jt));
}
@Override
public PersistentEntityCollectionJoin join(CollectionAttribute super E, Y> collection) {
return getCollectionJoin(collection.getName(), null);
}
@Override
public PersistentEntitySetJoin join(SetAttribute super E, Y> set) {
return getSetJoin(set.getName(), null);
}
@Override
public PersistentEntityListJoin join(ListAttribute super E, Y> list) {
return getListJoin(list.getName(), null);
}
@Override
public MapJoin join(MapAttribute super E, K, V> map) {
throw notSupportedOperation();
}
@Override
public PersistentEntitySetJoin join(SetAttribute super E, Y> set, JoinType jt) {
return getSetJoin(set.getName(), convert(Objects.requireNonNull(jt)));
}
@Override
public PersistentEntityListJoin join(ListAttribute super E, Y> list, JoinType jt) {
return getListJoin(list.getName(), convert(Objects.requireNonNull(jt)));
}
@Override
public MapJoin join(MapAttribute super E, K, V> map, JoinType jt) {
throw notSupportedOperation();
}
@Override
public PersistentEntityCollectionJoin joinCollection(String attributeName) {
return (PersistentEntityCollectionJoin) getCollectionJoin(attributeName, null);
}
@Override
public PersistentEntitySetJoin joinSet(String attributeName) {
return (PersistentEntitySetJoin) getSetJoin(attributeName, null);
}
@Override
public PersistentEntityListJoin joinList(String attributeName) {
return (PersistentEntityListJoin) getListJoin(attributeName, null);
}
@Override
public MapJoin joinMap(String attributeName) {
throw notSupportedOperation();
}
@Override
public PersistentEntityCollectionJoin joinCollection(String attributeName, JoinType jt) {
return (PersistentEntityCollectionJoin) getCollectionJoin(attributeName, null);
}
@Override
public PersistentEntitySetJoin joinSet(String attributeName, JoinType jt) {
return (PersistentEntitySetJoin) getSetJoin(attributeName, convert(Objects.requireNonNull(jt)));
}
@Override
public PersistentEntityListJoin joinList(String attributeName, JoinType jt) {
return (PersistentEntityListJoin) getListJoin(attributeName, convert(Objects.requireNonNull(jt)));
}
@Override
public MapJoin joinMap(String attributeName, JoinType jt) {
throw notSupportedOperation();
}
@Override
public Set> getJoins() {
return new LinkedHashSet<>(joins.values());
}
@Override
public Collection> getPersistentJoins() {
return joins.values();
}
@Override
public boolean isCorrelated() {
throw notSupportedOperation();
}
@Override
public From getCorrelationParent() {
throw notSupportedOperation();
}
@Override
public Set> getFetches() {
throw notSupportedOperation();
}
@Override
public Fetch fetch(SingularAttribute super E, Y> attribute) {
throw notSupportedOperation();
}
@Override
public Fetch fetch(SingularAttribute super E, Y> attribute, JoinType jt) {
throw notSupportedOperation();
}
@Override
public Fetch fetch(PluralAttribute super E, ?, Y> attribute) {
throw notSupportedOperation();
}
@Override
public Fetch fetch(PluralAttribute super E, ?, Y> attribute, JoinType jt) {
throw notSupportedOperation();
}
@Override
public Fetch fetch(String attributeName) {
throw notSupportedOperation();
}
@Override
public Fetch fetch(String attributeName, JoinType jt) {
throw notSupportedOperation();
}
@Override
public Path get(SingularAttribute super E, Y> attribute) {
return get(attribute.getName());
}
@Override
public Expression> type() {
throw notSupportedOperation();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy