下面列出了javax.persistence.JoinTable#javax.persistence.metamodel.PluralAttribute 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
@SuppressWarnings({ "unchecked" })
public ListAttribute<? super X, ?> getList(String name) {
PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
basicListCheck( attribute, name );
return (ListAttribute<? super X, ?>) attribute;
}
public Builder<X> getBuilder() {
if ( locked ) {
throw new IllegalStateException( "Type has been locked" );
}
return new Builder<X>() {
@Override
@SuppressWarnings("unchecked")
public void addAttribute(Attribute<X,?> attribute) {
declaredAttributes.put( attribute.getName(), attribute );
final Bindable.BindableType bindableType = ( ( Bindable ) attribute ).getBindableType();
switch ( bindableType ) {
case SINGULAR_ATTRIBUTE : {
declaredSingularAttributes.put( attribute.getName(), (SingularAttribute<X,?>) attribute );
break;
}
case PLURAL_ATTRIBUTE : {
declaredPluralAttributes.put(attribute.getName(), (PluralAttribute<X,?,?>) attribute );
break;
}
default : {
throw new AssertionFailure( "unknown bindable type: " + bindableType );
}
}
}
};
}
private <E> void checkTypeForPluralAttributes(
String attributeType,
PluralAttribute<?,?,?> attribute,
String name,
Class<E> elementType,
PluralAttribute.CollectionType collectionType) {
if ( attribute == null
|| ( elementType != null && !attribute.getBindableJavaType().equals( elementType ) )
|| attribute.getCollectionType() != collectionType ) {
throw new IllegalArgumentException(
attributeType + " named " + name
+ ( elementType != null ? " and of element type " + elementType : "" )
+ " is not present"
);
}
}
public static PluralAttribute.CollectionType determineCollectionType(Class javaType) {
if ( java.util.List.class.isAssignableFrom( javaType ) ) {
return PluralAttribute.CollectionType.LIST;
}
else if ( java.util.Set.class.isAssignableFrom( javaType ) ) {
return PluralAttribute.CollectionType.SET;
}
else if ( java.util.Map.class.isAssignableFrom( javaType ) ) {
return PluralAttribute.CollectionType.MAP;
}
else if ( java.util.Collection.class.isAssignableFrom( javaType ) ) {
return PluralAttribute.CollectionType.COLLECTION;
}
else if ( javaType.isArray() ) {
return PluralAttribute.CollectionType.LIST;
}
else {
throw new IllegalArgumentException( "Expecting collection type [" + javaType.getName() + "]" );
}
}
@Override
@SuppressWarnings({ "unchecked" })
public <Y> Path<Y> get(String attributeName) {
if ( ! canBeDereferenced() ) {
throw illegalDereference();
}
final Attribute attribute = locateAttribute( attributeName );
if ( attribute.isCollection() ) {
final PluralAttribute<X,Y,?> pluralAttribute = (PluralAttribute<X,Y,?>) attribute;
if ( PluralAttribute.CollectionType.MAP.equals( pluralAttribute.getCollectionType() ) ) {
return (PluralAttributePath<Y>) this.<Object,Object,Map<Object, Object>>get( (MapAttribute) pluralAttribute );
}
else {
return (PluralAttributePath<Y>) this.get( (PluralAttribute) pluralAttribute );
}
}
else {
return get( (SingularAttribute<X,Y>) attribute );
}
}
@SuppressWarnings("unchecked")
public <E, F> Path<F> getPath(Root<E> root, List<Attribute<?, ?>> attributes) {
Path<?> path = root;
for (Attribute<?, ?> attribute : attributes) {
boolean found = false;
if (path instanceof FetchParent) {
for (Fetch<E, ?> fetch : ((FetchParent<?, E>) path).getFetches()) {
if (attribute.getName().equals(fetch.getAttribute().getName()) && (fetch instanceof Join<?, ?>)) {
path = (Join<E, ?>) fetch;
found = true;
break;
}
}
}
if (!found) {
if (attribute instanceof PluralAttribute) {
path = ((From<?, ?>) path).join(attribute.getName(), JoinType.LEFT);
} else {
path = path.get(attribute.getName());
}
}
}
return (Path<F>) path;
}
@SuppressWarnings({"unchecked", "rawtypes"})
protected void fetches(SearchParameters sp, Root<E> root) {
for (List<Attribute<?, ?>> args : sp.getFetches()) {
FetchParent<?, ?> from = root;
for (Attribute<?, ?> arg : args) {
boolean found = false;
for (Fetch<?, ?> fetch : from.getFetches()) {
if (arg.equals(fetch.getAttribute())) {
from = fetch;
found = true;
break;
}
}
if (!found) {
if (arg instanceof PluralAttribute) {
from = from.fetch((PluralAttribute) arg, JoinType.LEFT);
} else {
from = from.fetch((SingularAttribute) arg, JoinType.LEFT);
}
}
}
}
}
public List<Attribute<?, ?>> toAttributes(String path, Class<?> from) {
try {
List<Attribute<?, ?>> attributes = newArrayList();
Class<?> current = from;
for (String pathItem : Splitter.on(".").split(path)) {
Class<?> metamodelClass = getCachedClass(current);
Field field = metamodelClass.getField(pathItem);
Attribute<?, ?> attribute = (Attribute<?, ?>) field.get(null);
attributes.add(attribute);
if (attribute instanceof PluralAttribute) {
current = ((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType();
} else {
current = attribute.getJavaType();
}
}
return attributes;
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
@Override
@SuppressWarnings({ "unchecked" })
public Set<PluralAttribute<? super X, ?, ?>> getPluralAttributes() {
HashSet attributes = new HashSet<PluralAttribute<? super X, ?, ?>>( declaredPluralAttributes.values() );
if ( getSupertype() != null ) {
attributes.addAll( getSupertype().getPluralAttributes() );
}
return attributes;
}
@Override
@SuppressWarnings({ "unchecked" })
public CollectionAttribute<? super X, ?> getCollection(String name) {
PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
basicCollectionCheck( attribute, name );
return ( CollectionAttribute<X, ?> ) attribute;
}
@Override
@SuppressWarnings( "unchecked")
public CollectionAttribute<X, ?> getDeclaredCollection(String name) {
final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
basicCollectionCheck( attribute, name );
return ( CollectionAttribute<X, ?> ) attribute;
}
@Override
@SuppressWarnings({ "unchecked" })
public SetAttribute<? super X, ?> getSet(String name) {
PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
basicSetCheck( attribute, name );
return (SetAttribute<? super X, ?>) attribute;
}
@Override
@SuppressWarnings( "unchecked")
public SetAttribute<X, ?> getDeclaredSet(String name) {
final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
basicSetCheck( attribute, name );
return ( SetAttribute<X, ?> ) attribute;
}
@Override
@SuppressWarnings("unchecked")
public ListAttribute<X, ?> getDeclaredList(String name) {
final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
basicListCheck( attribute, name );
return ( ListAttribute<X, ?> ) attribute;
}
@Override
@SuppressWarnings({ "unchecked" })
public MapAttribute<? super X, ?, ?> getMap(String name) {
PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
basicMapCheck( attribute, name );
return (MapAttribute<? super X, ?, ?>) attribute;
}
@Override
@SuppressWarnings("unchecked")
public MapAttribute<X, ?, ?> getDeclaredMap(String name) {
final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
basicMapCheck( attribute, name );
return ( MapAttribute<X,?,?> ) attribute;
}
@Override
@SuppressWarnings({ "unchecked" })
public <E> CollectionAttribute<? super X, E> getCollection(String name, Class<E> elementType) {
PluralAttribute<? super X, ?, ?> attribute = declaredPluralAttributes.get( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
checkCollectionElementType( attribute, name, elementType );
return ( CollectionAttribute<? super X, E> ) attribute;
}
@Override
@SuppressWarnings({ "unchecked" })
public <E> SetAttribute<? super X, E> getSet(String name, Class<E> elementType) {
PluralAttribute<? super X, ?, ?> attribute = declaredPluralAttributes.get( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
checkSetElementType( attribute, name, elementType );
return ( SetAttribute<? super X, E> ) attribute;
}
@Override
@SuppressWarnings("unchecked")
public <E> SetAttribute<X, E> getDeclaredSet(String name, Class<E> elementType) {
final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
checkSetElementType( attribute, name, elementType );
return ( SetAttribute<X, E> ) attribute;
}
@Override
@SuppressWarnings("unchecked")
public <E> ListAttribute<X, E> getDeclaredList(String name, Class<E> elementType) {
final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
checkListElementType( attribute, name, elementType );
return ( ListAttribute<X, E> ) attribute;
}
@Override
@SuppressWarnings({ "unchecked" })
public <K, V> MapAttribute<? super X, K, V> getMap(String name, Class<K> keyType, Class<V> valueType) {
PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
checkMapValueType( attribute, name, valueType );
final MapAttribute<? super X, K, V> mapAttribute = ( MapAttribute<? super X, K, V> ) attribute;
checkMapKeyType( mapAttribute, name, keyType );
return mapAttribute;
}
@Override
@SuppressWarnings("unchecked")
public <K, V> MapAttribute<X, K, V> getDeclaredMap(String name, Class<K> keyType, Class<V> valueType) {
final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
checkMapValueType( attribute, name, valueType );
final MapAttribute<X, K, V> mapAttribute = ( MapAttribute<X, K, V> ) attribute;
checkMapKeyType( mapAttribute, name, keyType );
return mapAttribute;
}
@Override
@SuppressWarnings("unchecked")
protected ManagedType<? super X> locateManagedType() {
if ( getModel().getBindableType() == Bindable.BindableType.ENTITY_TYPE ) {
return (ManagedType<? super X>) getModel();
}
else if ( getModel().getBindableType() == Bindable.BindableType.SINGULAR_ATTRIBUTE ) {
final Type joinedAttributeType = ( (SingularAttribute) getAttribute() ).getType();
if ( !ManagedType.class.isInstance( joinedAttributeType ) ) {
throw new UnsupportedOperationException(
"Cannot further dereference attribute join [" + getPathIdentifier() + "] as its type is not a ManagedType"
);
}
return (ManagedType<? super X>) joinedAttributeType;
}
else if ( getModel().getBindableType() == Bindable.BindableType.PLURAL_ATTRIBUTE ) {
final Type elementType = ( (PluralAttribute) getAttribute() ).getElementType();
if ( !ManagedType.class.isInstance( elementType ) ) {
throw new UnsupportedOperationException(
"Cannot further dereference attribute join [" + getPathIdentifier() + "] (plural) as its element type is not a ManagedType"
);
}
return (ManagedType<? super X>) elementType;
}
return super.locateManagedType();
}
@Override
@SuppressWarnings({"unchecked"})
public <X, Y> CollectionJoin<X, Y> joinCollection(String attributeName, JoinType jt) {
final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute( attributeName );
if ( !attribute.isCollection() ) {
throw new IllegalArgumentException( "Requested attribute was not a collection" );
}
final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
if ( !PluralAttribute.CollectionType.COLLECTION.equals( pluralAttribute.getCollectionType() ) ) {
throw new IllegalArgumentException( "Requested attribute was not a collection" );
}
return (CollectionJoin<X, Y>) join( (CollectionAttribute) attribute, jt );
}
@Override
@SuppressWarnings({"unchecked"})
public <X, Y> SetJoin<X, Y> joinSet(String attributeName, JoinType jt) {
final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute( attributeName );
if ( !attribute.isCollection() ) {
throw new IllegalArgumentException( "Requested attribute was not a set" );
}
final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
if ( !PluralAttribute.CollectionType.SET.equals( pluralAttribute.getCollectionType() ) ) {
throw new IllegalArgumentException( "Requested attribute was not a set" );
}
return (SetJoin<X, Y>) join( (SetAttribute) attribute, jt );
}
@Override
@SuppressWarnings({"unchecked"})
public <X, Y> ListJoin<X, Y> joinList(String attributeName, JoinType jt) {
final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute( attributeName );
if ( !attribute.isCollection() ) {
throw new IllegalArgumentException( "Requested attribute was not a list" );
}
final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
if ( !PluralAttribute.CollectionType.LIST.equals( pluralAttribute.getCollectionType() ) ) {
throw new IllegalArgumentException( "Requested attribute was not a list" );
}
return (ListJoin<X, Y>) join( (ListAttribute) attribute, jt );
}
@Override
@SuppressWarnings({"unchecked"})
public <X, Y> Fetch<X, Y> fetch(String attributeName, JoinType jt) {
if ( !canBeFetchSource() ) {
throw illegalFetch();
}
Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute( attributeName );
if ( attribute.isCollection() ) {
return (Fetch<X, Y>) fetch( (PluralAttribute) attribute, jt );
}
else {
return (Fetch<X, Y>) fetch( (SingularAttribute) attribute, jt );
}
}
public PluralAttributePath(
CriteriaBuilderImpl criteriaBuilder,
PathSource source,
PluralAttribute<?,X,?> attribute) {
super( criteriaBuilder, attribute.getJavaType(), source );
this.attribute = attribute;
this.persister = resolvePersister( criteriaBuilder, attribute );
}
@Override
@SuppressWarnings({ "unchecked" })
public <E, C extends Collection<E>> Expression<C> get(PluralAttribute<X, C, E> attribute) {
if ( ! canBeDereferenced() ) {
throw illegalDereference();
}
PluralAttributePath<C> path = (PluralAttributePath<C>) resolveCachedAttributePath( attribute.getName() );
if ( path == null ) {
path = new PluralAttributePath<C>( criteriaBuilder(), this, attribute );
registerAttributePath( attribute.getName(), path );
}
return path;
}
private Predicate getPredicate(CriteriaBuilder cb, Root root, DataFetchingEnvironment environment, Argument argument) {
Path path = null;
if (!argument.getName().contains(".")) {
Attribute argumentEntityAttribute = getAttribute(environment, argument);
// If the argument is a list, let's assume we need to join and do an 'in' clause
if (argumentEntityAttribute instanceof PluralAttribute) {
Join join = root.join(argument.getName());
return join.in(convertValue(environment, argument, argument.getValue()));
}
path = root.get(argument.getName());
return cb.equal(path, convertValue(environment, argument, argument.getValue()));
} else {
List<String> parts = Arrays.asList(argument.getName().split("\\."));
for (String part : parts) {
if (path == null) {
path = root.get(part);
} else {
path = path.get(part);
}
}
return cb.equal(path, convertValue(environment, argument, argument.getValue()));
}
}