org.hibernate.query.criteria.internal.path.MapKeyHelpers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
JPMS Module-Info's for a few of the Jakarta Libraries just until they add them in themselves
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.query.criteria.internal.path;
import java.io.Serializable;
import java.lang.reflect.Member;
import java.util.Map;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.hibernate.query.criteria.internal.MapJoinImplementor;
import org.hibernate.query.criteria.internal.PathImplementor;
import org.hibernate.query.criteria.internal.PathSource;
import org.hibernate.query.criteria.internal.compile.RenderingContext;
import org.hibernate.persister.collection.CollectionPersister;
/**
* {@link javax.persistence.criteria.MapJoin#key} poses a number of implementation difficulties in terms of the
* type signatures amongst the {@link javax.persistence.criteria.Path}, {@link javax.persistence.criteria.Join} and
* {@link Attribute}. The implementations found here provide that bridge.
*
* @author Steve Ebersole
*/
public class MapKeyHelpers {
/**
* Disallow instantiation
*/
private MapKeyHelpers() {
}
/**
* Models a path to a map key. This is the actual return used from {@link javax.persistence.criteria.MapJoin#key}
*
* @param The type of the map key.
*/
public static class MapKeyPath
extends AbstractPathImpl
implements PathImplementor, Serializable {
private final MapKeyAttribute mapKeyAttribute;
public MapKeyPath(
CriteriaBuilderImpl criteriaBuilder,
MapKeySource source,
MapKeyAttribute mapKeyAttribute) {
super( criteriaBuilder, mapKeyAttribute.getJavaType(), source );
this.mapKeyAttribute = mapKeyAttribute;
}
@Override
public MapKeySource getPathSource() {
return (MapKeySource) super.getPathSource();
}
public MapKeyAttribute getAttribute() {
return mapKeyAttribute;
}
private boolean isBasicTypeKey() {
return Attribute.PersistentAttributeType.BASIC ==
mapKeyAttribute.getPersistentAttributeType();
}
@Override
protected boolean canBeDereferenced() {
return ! isBasicTypeKey();
}
@Override
protected Attribute locateAttributeInternal(String attributeName) {
if ( ! canBeDereferenced() ) {
throw new IllegalArgumentException(
"Map key [" + getPathSource().getPathIdentifier() + "] cannot be dereferenced"
);
}
throw new UnsupportedOperationException( "Not yet supported!" );
}
public Bindable getModel() {
return mapKeyAttribute;
}
@Override
@SuppressWarnings("unchecked")
public MapKeyPath treatAs(Class treatAsType) {
// todo : if key is an entity, this is probably not enough
return (MapKeyPath) this;
}
@Override
public String render(RenderingContext renderingContext) {
PathSource source = getPathSource();
String name;
if ( source != null ) {
source.prepareAlias( renderingContext );
name = source.getPathIdentifier();
}
else {
name = getAttribute().getName();
}
return "key(" + name + ")";
}
}
/**
* Defines a path for the map which can then be used to represent the source of the
* map key "attribute".
*
* @param The map key type
* @param The map value type
*/
public static class MapKeySource
extends AbstractPathImpl