javax.persistence.metamodel.PluralAttribute Java Examples

The following examples show how to use javax.persistence.metamodel.PluralAttribute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 7 votes vote down vote up
@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;
}
 
Example #2
Source File: AttributeFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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() + "]" );
	}
}
 
Example #3
Source File: GenericRepository.java    From javaee-lab with Apache License 2.0 6 votes vote down vote up
@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);
                }
            }
        }
    }
}
 
Example #4
Source File: MetamodelUtil.java    From javaee-lab with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #5
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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"
		);
	}
}
 
Example #6
Source File: JpaUtil.java    From javaee-lab with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #7
Source File: AbstractPathImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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 );
	}
}
 
Example #8
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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 );
				}
			}
		}
	};
}
 
Example #9
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #10
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings( "unchecked")
public CollectionAttribute<X, ?> getDeclaredCollection(String name) {
	final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
	basicCollectionCheck( attribute, name );
	return ( CollectionAttribute<X, ?> ) attribute;
}
 
Example #11
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public MapAttribute<X, ?, ?> getDeclaredMap(String name) {
	final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
	basicMapCheck( attribute, name );
	return ( MapAttribute<X,?,?> ) attribute;
}
 
Example #12
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public ListAttribute<X, ?> getDeclaredList(String name) {
	final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
	basicListCheck( attribute, name );
	return ( ListAttribute<X, ?> ) attribute;
}
 
Example #13
Source File: JpaDataFetcher.java    From graphql-jpa with MIT License 5 votes vote down vote up
private Class getJavaType(DataFetchingEnvironment environment, Argument argument) {
    Attribute argumentEntityAttribute = getAttribute(environment, argument);

    if (argumentEntityAttribute instanceof PluralAttribute)
        return ((PluralAttribute) argumentEntityAttribute).getElementType().getJavaType();

    return argumentEntityAttribute.getJavaType();
}
 
Example #14
Source File: AbstractPathImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #15
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #16
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #17
Source File: JPAEdmNameBuilder.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
public static void build(final JPAEdmAssociationEndView assocaitionEndView,
    final JPAEdmEntityTypeView entityTypeView, final JPAEdmPropertyView propertyView) {

  String namespace = buildNamespace(assocaitionEndView);

  String name = entityTypeView.getEdmEntityType().getName();
  FullQualifiedName fQName = new FullQualifiedName(namespace, name);
  assocaitionEndView.getEdmAssociationEnd1().setType(fQName);

  name = null;
  String jpaEntityTypeName = null;
  Attribute<?, ?> jpaAttribute = propertyView.getJPAAttribute();
  if (jpaAttribute.isCollection()) {
    jpaEntityTypeName = ((PluralAttribute<?, ?, ?>) jpaAttribute).getElementType().getJavaType()
        .getSimpleName();
  } else {
    jpaEntityTypeName = propertyView.getJPAAttribute().getJavaType()
        .getSimpleName();
  }

  JPAEdmMappingModelAccess mappingModelAccess = assocaitionEndView
      .getJPAEdmMappingModelAccess();

  if (mappingModelAccess != null
      && mappingModelAccess.isMappingModelExists()) {
    name = mappingModelAccess.mapJPAEntityType(jpaEntityTypeName);
  }

  if (name == null) {
    name = jpaEntityTypeName;
  }

  fQName = new FullQualifiedName(namespace, name);
  assocaitionEndView.getEdmAssociationEnd2().setType(fQName);

}
 
Example #18
Source File: JpaDataFetcher.java    From graphql-jpa with MIT License 5 votes vote down vote up
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()));
    }
}
 
Example #19
Source File: AbstractFromImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 );
	}
}
 
Example #20
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #21
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #22
Source File: MetamodelUtil.java    From javaee-lab with Apache License 2.0 5 votes vote down vote up
public boolean isOrphanRemoval(PluralAttribute<?, ?, ?> attribute) {
    if (attribute.getJavaMember() instanceof AccessibleObject) {
        AccessibleObject accessibleObject = (AccessibleObject) attribute.getJavaMember();
        OneToMany oneToMany = accessibleObject.getAnnotation(OneToMany.class);
        if (oneToMany != null) {
            return oneToMany.orphanRemoval();
        }
    }
    return true;
}
 
Example #23
Source File: SingularAttributeJoin.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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();
}
 
Example #24
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #25
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #26
Source File: AbstractFromImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 );
}
 
Example #27
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #28
Source File: AbstractFromImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 );
}
 
Example #29
Source File: AbstractFromImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 );
}
 
Example #30
Source File: AbstractManagedType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}