Java Code Examples for javax.persistence.metamodel.Attribute#isAssociation()

The following examples show how to use javax.persistence.metamodel.Attribute#isAssociation() . 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: QueryUtil.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
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 2
Source File: QueryUtil.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
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: CustomSpecifications.java    From spring-boot-rest-api-helpers with MIT License 5 votes vote down vote up
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 4
Source File: CustomSpecifications.java    From spring-boot-rest-api-helpers with MIT License 5 votes vote down vote up
private Class getJavaTypeOfClassContainingAttribute(Root root, String attributeName) {
    Attribute a = root.getModel().getAttribute(attributeName);
    if (a.isAssociation()) {
        return addJoinIfNotExists(root, a, false, false).getJavaType();
    }
    return null;
}
 
Example 5
Source File: QueryUtil.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
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 6
Source File: QueryUtil.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
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 7
Source File: JpaUtils.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * 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 8
Source File: JpaUtils.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the mappedBy value from an attribute
 * @param attribute attribute
 * @return mappedBy value or null if none.
 */
public static String getMappedBy(Attribute<?, ?> attribute) {
	String mappedBy = null;
	
	if (attribute.isAssociation()) {
		Annotation[] annotations = null;
		Member member = attribute.getJavaMember();
		if (member instanceof Field) {
			annotations = ((Field) member).getAnnotations();
		}
		else if (member instanceof Method) {
			annotations = ((Method) member).getAnnotations();
		}
		
		for (Annotation a : annotations) {
			if (a.annotationType().equals(OneToMany.class)) {
				mappedBy = ((OneToMany) a).mappedBy();
				break;
			}
			else if (a.annotationType().equals(ManyToMany.class)) {
				mappedBy = ((ManyToMany) a).mappedBy();
				break;
			}
			else if (a.annotationType().equals(OneToOne.class)) {
				mappedBy = ((OneToOne) a).mappedBy();
				break;
			}
		}
	}
	
	return "".equals(mappedBy) ? null : mappedBy;
}
 
Example 9
Source File: CustomSpecifications.java    From spring-boot-rest-api-helpers with MIT License 4 votes vote down vote up
public Predicate handleAllCases(CriteriaBuilder builder, Root root, Join join, CriteriaQuery query, Attribute a, String key, Object val) {
    boolean isValueCollection = val instanceof Collection;
    boolean isValueMap = val instanceof Map;
    String cleanKey = cleanUpKey(key);
    boolean isKeyClean = cleanKey.equals(key);
    boolean isNegation = key.endsWith("Not");
    boolean isGt = key.endsWith("Gt");
    boolean isGte = key.endsWith("Gte");
    boolean isLt = key.endsWith("Lt");
    boolean isLte = key.endsWith("Lte");
    boolean isConjunction = key.endsWith("And");
    boolean isAssociation = a.isAssociation();

    if (isValueMap) {
        val = convertMapContainingPrimaryIdToValue(val, a, root);
    }
    if (val instanceof Map && isAssociation) {
        List<Predicate> predicates =  handleMap(builder, root, addJoinIfNotExists(root,a, isValueCollection, isConjunction), query, ((Map)val), Arrays.asList());
        Predicate[] predicatesArray = predicates.toArray(new Predicate[predicates.size()]);
        return  builder.and(predicatesArray);
    }



    if (isKeyClean) {
        return handleCleanKeyCase(builder, root, join, query, cleanKey, a,  val);
    } else if (isNegation) {
        return builder.not(handleCleanKeyCase(builder, root, join, query, cleanKey, a,  val));
    } else if (isConjunction) {
        if (isValueCollection) {
            return handleCollection(builder, root, join, query, a,  cleanKey, (Collection) val, true);
        }
    } else if (isLte) {
        return createLtePredicate(builder, root, a, val);
    } else if (isGte) {
        return createGtePredicate(builder, root, a, val);
    } else if (isLt) {
        return createLtPredicate(builder, root, a, val);
    } else if (isGt) {
        return createGtPredicate(builder, root, a, val);
    }
    return builder.conjunction();
}
 
Example 10
Source File: CustomSpecifications.java    From spring-boot-rest-api-helpers with MIT License 4 votes vote down vote up
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");
}