Java Code Examples for javax.persistence.metamodel.Attribute#isCollection()
The following examples show how to use
javax.persistence.metamodel.Attribute#isCollection() .
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: AbstractPathImpl.java From lams with GNU General Public License v2.0 | 6 votes |
@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 2
Source File: QueryUtil.java From crnk-framework with Apache License 2.0 | 5 votes |
private static boolean containsMultiRelationJoin(Set<?> fetches) { for (Object fetchObj : fetches) { Fetch<?, ?> fetch = (Fetch<?, ?>) fetchObj; Attribute<?, ?> attr = fetch.getAttribute(); if (attr.isAssociation() && attr.isCollection()) return true; if (containsMultiRelationFetch(fetch.getFetches())) return true; } return false; }
Example 3
Source File: JPAEdmNameBuilder.java From cloud-odata-java with Apache License 2.0 | 5 votes |
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 4
Source File: JpaUtils.java From jdal with Apache License 2.0 | 5 votes |
/** * Test if attribute is type or in collections has element type * @param attribute attribute to test * @param clazz Class to test * @return true if clazz is asignable from type or element type */ public static boolean isTypeOrElementType(Attribute<?, ?> attribute, Class<?> clazz) { if (attribute.isCollection()) { return clazz.isAssignableFrom(((CollectionAttribute<?, ?>) attribute).getBindableJavaType()); } return clazz.isAssignableFrom(attribute.getJavaType()); }
Example 5
Source File: JpaUtils.java From jdal with Apache License 2.0 | 5 votes |
/** * Initialize a entity. * @param em entity manager to use * @param entity entity to initialize * @param depth max depth on recursion */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void initialize(EntityManager em, Object entity, int depth) { // return on nulls, depth = 0 or already initialized objects if (entity == null || depth == 0) { return; } PersistenceUnitUtil unitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil(); EntityType entityType = em.getMetamodel().entity(entity.getClass()); Set<Attribute> attributes = entityType.getDeclaredAttributes(); Object id = unitUtil.getIdentifier(entity); if (id != null) { Object attached = em.find(entity.getClass(), unitUtil.getIdentifier(entity)); for (Attribute a : attributes) { if (!unitUtil.isLoaded(entity, a.getName())) { if (a.isCollection()) { intializeCollection(em, entity, attached, a, depth); } else if(a.isAssociation()) { intialize(em, entity, attached, a, depth); } } } } }
Example 6
Source File: JpaSpecificationExecutorWithProjectionImpl.java From specification-with-projection with MIT License | 5 votes |
private static boolean requiresJoin(@Nullable Bindable<?> propertyPathModel, boolean isPluralAttribute, boolean isLeafProperty, boolean isForSelection) { if (propertyPathModel == null && isPluralAttribute) { return true; } if (!(propertyPathModel instanceof Attribute)) { return false; } Attribute<?, ?> attribute = (Attribute<?, ?>) propertyPathModel; if (!ASSOCIATION_TYPES.containsKey(attribute.getPersistentAttributeType())) { return false; } // if this path is part of the select list we need to generate an explicit outer join in order to prevent Hibernate // to use an inner join instead. // see https://hibernate.atlassian.net/browse/HHH-12999. if (isLeafProperty && !isForSelection && !attribute.isCollection()) { return false; } Class<? extends Annotation> associationAnnotation = ASSOCIATION_TYPES.get(attribute.getPersistentAttributeType()); if (associationAnnotation == null) { return true; } Member member = attribute.getJavaMember(); if (!(member instanceof AnnotatedElement)) { return true; } Annotation annotation = AnnotationUtils.getAnnotation((AnnotatedElement) member, associationAnnotation); return annotation == null ? true : (boolean) AnnotationUtils.getValue(annotation, "optional"); }
Example 7
Source File: QueryUtil.java From katharsis-framework with Apache License 2.0 | 5 votes |
private static boolean containsMultiRelationJoin(Set<?> fetches) { for (Object fetchObj : fetches) { Fetch<?, ?> fetch = (Fetch<?, ?>) fetchObj; Attribute<?, ?> attr = fetch.getAttribute(); if (attr.isAssociation() && attr.isCollection()) return true; if (containsMultiRelationFetch(fetch.getFetches())) return true; } return false; }
Example 8
Source File: QueryUtil.java From katharsis-framework with Apache License 2.0 | 5 votes |
private static boolean containsMultiRelationFetch(Set<?> fetches) { for (Object fetchObj : fetches) { Fetch<?, ?> fetch = (Fetch<?, ?>) fetchObj; Attribute<?, ?> attr = fetch.getAttribute(); if (attr.isAssociation() && attr.isCollection()) return true; if (containsMultiRelationFetch(fetch.getFetches())) return true; } return false; }
Example 9
Source File: CustomSpecifications.java From spring-boot-rest-api-helpers with MIT License | 5 votes |
public Predicate handleCleanKeyCase(CriteriaBuilder builder, Root root, Join join, CriteriaQuery query, String key, Attribute a, Object val) { boolean isValueCollection = val instanceof Collection; boolean isValTextSearch = (val instanceof String) && ((String) val).contains("%"); if (isValueCollection) { return handleCollection(builder, root, join, query, a, key, (Collection) val, false); } else if (isValTextSearch) { return createLikePredicate(builder, root, join, a, (String) val); } else if(a.isCollection() && !a.isAssociation()) { return createEqualityPredicate(builder, root, addJoinIfNotExists(root, a, false, isValueCollection), a, val); } else { return createEqualityPredicate(builder, root, join, a, val); } }
Example 10
Source File: AbstractFromImpl.java From lams with GNU General Public License v2.0 | 5 votes |
@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 11
Source File: AbstractFromImpl.java From lams with GNU General Public License v2.0 | 5 votes |
@Override @SuppressWarnings({"unchecked"}) public <X, K, V> MapJoin<X, K, V> joinMap(String attributeName, JoinType jt) { final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute( attributeName ); if ( !attribute.isCollection() ) { throw new IllegalArgumentException( "Requested attribute was not a map" ); } final PluralAttribute pluralAttribute = (PluralAttribute) attribute; if ( !PluralAttribute.CollectionType.MAP.equals( pluralAttribute.getCollectionType() ) ) { throw new IllegalArgumentException( "Requested attribute was not a map" ); } return (MapJoin<X, K, V>) join( (MapAttribute) attribute, jt ); }
Example 12
Source File: AbstractFromImpl.java From lams with GNU General Public License v2.0 | 5 votes |
@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 13
Source File: AbstractFromImpl.java From lams with GNU General Public License v2.0 | 5 votes |
@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 14
Source File: AbstractFromImpl.java From lams with GNU General Public License v2.0 | 5 votes |
@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 15
Source File: AbstractFromImpl.java From lams with GNU General Public License v2.0 | 5 votes |
@Override @SuppressWarnings({"unchecked"}) public <X, Y> Join<X, Y> join(String attributeName, JoinType jt) { if ( !canBeJoinSource() ) { throw illegalJoin(); } if ( jt.equals( JoinType.RIGHT ) ) { throw new UnsupportedOperationException( "RIGHT JOIN not supported" ); } final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute( attributeName ); if ( attribute.isCollection() ) { final PluralAttribute pluralAttribute = (PluralAttribute) attribute; if ( PluralAttribute.CollectionType.COLLECTION.equals( pluralAttribute.getCollectionType() ) ) { return (Join<X, Y>) join( (CollectionAttribute) attribute, jt ); } else if ( PluralAttribute.CollectionType.LIST.equals( pluralAttribute.getCollectionType() ) ) { return (Join<X, Y>) join( (ListAttribute) attribute, jt ); } else if ( PluralAttribute.CollectionType.SET.equals( pluralAttribute.getCollectionType() ) ) { return (Join<X, Y>) join( (SetAttribute) attribute, jt ); } else { return (Join<X, Y>) join( (MapAttribute) attribute, jt ); } } else { return (Join<X, Y>) join( (SingularAttribute) attribute, jt ); } }
Example 16
Source File: QueryUtil.java From crnk-framework with Apache License 2.0 | 5 votes |
private static boolean containsMultiRelationFetch(Set<?> fetches) { for (Object fetchObj : fetches) { Fetch<?, ?> fetch = (Fetch<?, ?>) fetchObj; Attribute<?, ?> attr = fetch.getAttribute(); if (attr.isAssociation() && attr.isCollection()) return true; if (containsMultiRelationFetch(fetch.getFetches())) return true; } return false; }
Example 17
Source File: CustomSpecifications.java From spring-boot-rest-api-helpers with MIT License | 4 votes |
private Predicate createEqualityPredicate(CriteriaBuilder builder, Root root, Join join, Attribute a, Object val) { if (isNull(a, val)) { if (a.isAssociation() && a.isCollection()) { return builder.isEmpty(root.get(a.getName())); } else if(isPrimitive(a)) { return builder.isNull(root.get(a.getName())); } else { return root.get(a.getName()).isNull(); } } else if (join == null) { if (isEnum(a)) { return builder.equal(root.get(a.getName()), Enum.valueOf(Class.class.cast(a.getJavaType()), (String) val)); } else if (isPrimitive(a)) { return builder.equal(root.get(a.getName()), val); } else if(isUUID(a)) { return builder.equal(root.get(a.getName()), UUID.fromString(val.toString())); } else if(a.isAssociation()) { if (isPrimaryKeyOfAttributeUUID(a, root)) { return prepareJoinAssociatedPredicate(builder, root, a, UUID.fromString(val.toString())); } else { return prepareJoinAssociatedPredicate(builder, root, a, val); } } } else if (join != null) { if (isEnum(a)) { return builder.equal(join.get(a.getName()), Enum.valueOf(Class.class.cast(a.getJavaType()), (String) val)); } else if (isPrimitive(a)) { return builder.equal(join.get(a.getName()), val); } else if (a.isAssociation()) { return builder.equal(join.get(a.getName()), val); } else if(a.isCollection()) { return builder.equal(join, val); } } throw new IllegalArgumentException("equality/inequality is currently supported on primitives and enums"); }
Example 18
Source File: JpaDao.java From jdal with Apache License 2.0 | 4 votes |
/** * Null References on one to many and one to one associations. * Will only work if association has annotated with a mappedBy attribute. * * @param entity entity */ private void nullReferences(T entity) { EntityType<T> type = em.getMetamodel().entity(getEntityClass()); if (log.isDebugEnabled()) log.debug("Null references on entity " + type.getName()); for (Attribute<?, ?> a : type.getAttributes()) { if (PersistentAttributeType.ONE_TO_MANY == a.getPersistentAttributeType() || PersistentAttributeType.ONE_TO_ONE == a.getPersistentAttributeType()) { Object association = PropertyAccessorFactory.forDirectFieldAccess(entity) .getPropertyValue(a.getName()); if (association != null) { EntityType<?> associationType = null; if (a.isCollection()) { associationType = em.getMetamodel().entity( ((PluralAttribute<?, ?, ?>)a).getBindableJavaType()); } else { associationType = em.getMetamodel().entity(a.getJavaType()); } String mappedBy = JpaUtils.getMappedBy(a); if (mappedBy != null) { Attribute<?,?> aa = associationType.getAttribute(mappedBy); if (PersistentAttributeType.MANY_TO_ONE == aa.getPersistentAttributeType()) { if (log.isDebugEnabled()) { log.debug("Null ManyToOne reference on " + associationType.getName() + "." + aa.getName()); } for (Object o : (Collection<?>) association) { PropertyAccessorFactory.forDirectFieldAccess(o).setPropertyValue(aa.getName(), null); } } else if (PersistentAttributeType.ONE_TO_ONE == aa.getPersistentAttributeType()) { if (log.isDebugEnabled()) { log.debug("Null OneToOne reference on " + associationType.getName() + "." + aa.getName()); } PropertyAccessorFactory.forDirectFieldAccess(association).setPropertyValue(aa.getName(), null); } } } } } }
Example 19
Source File: JPAEdmNameBuilder.java From cloud-odata-java with Apache License 2.0 | 4 votes |
public static void build(final JPAEdmAssociationView associationView, final JPAEdmPropertyView propertyView, final JPAEdmNavigationPropertyView navPropertyView, final int count) { String toName = null; String fromName = null; String navPropName = null; NavigationProperty navProp = navPropertyView.getEdmNavigationProperty(); String namespace = buildNamespace(associationView); Association association = associationView.getEdmAssociation(); navProp.setRelationship(new FullQualifiedName(namespace, association .getName())); FullQualifiedName associationEndTypeOne = association.getEnd1() .getType(); FullQualifiedName associationEndTypeTwo = association.getEnd2() .getType(); Attribute<?, ?> jpaAttribute = propertyView.getJPAAttribute(); navProp.setMapping(new Mapping().setInternalName(jpaAttribute.getName())); String jpaEntityTypeName = propertyView.getJPAEdmEntityTypeView() .getJPAEntityType().getName(); JPAEdmMappingModelAccess mappingModelAccess = navPropertyView .getJPAEdmMappingModelAccess(); String targetEntityTypeName = null; if (jpaAttribute.isCollection()) { targetEntityTypeName = ((PluralAttribute<?, ?, ?>) jpaAttribute).getElementType().getJavaType().getSimpleName(); } else { targetEntityTypeName = jpaAttribute.getJavaType().getSimpleName(); } if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) { navPropName = mappingModelAccess.mapJPARelationship( jpaEntityTypeName, jpaAttribute.getName()); toName = mappingModelAccess.mapJPAEntityType(targetEntityTypeName); fromName = mappingModelAccess .mapJPAEntityType(jpaEntityTypeName); } if (toName == null) { toName = targetEntityTypeName; } if (fromName == null) { fromName = jpaEntityTypeName; } if (navPropName == null) { navPropName = toName.concat(NAVIGATION_NAME); } if (count > 1) { navPropName = navPropName + Integer.toString(count - 1); } navProp.setName(navPropName); if (toName.equals(associationEndTypeOne.getName())) { navProp.setFromRole(association.getEnd2().getRole()); navProp.setToRole(association.getEnd1().getRole()); } else if (toName.equals(associationEndTypeTwo.getName())) { navProp.setToRole(association.getEnd2().getRole()); navProp.setFromRole(association.getEnd1().getRole()); } }