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

The following examples show how to use javax.persistence.metamodel.Attribute#getPersistentAttributeType() . 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: JPAQueryBuilder.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
/**
 * Verify via {@link EntityManager} if one of the attributes of the selected entity
 * contains a embedded attribute.
 * Return true if at least one embedded attribute is found or false if non embedded
 * attribute is found.
 *
 * @param em according entity manager
 * @param jpqlQuery query to verify
 * @return true if at least one embedded attribute is found or false if non embedded
 * attribute is found.
 */
private static boolean containsEmbeddedAttributes(EntityManager em, String jpqlQuery) {
  Set<EntityType<?>> types = em.getMetamodel().getEntities();
  int pos = jpqlQuery.indexOf("FROM ") + 5;
  int lastpos = jpqlQuery.indexOf(" ", pos);
  final String queriedEntity = jpqlQuery.substring(pos, lastpos);
  for (EntityType<?> type : types) {
    if(queriedEntity.equals(type.getName())) {
      Set<Attribute<?, ?>> attributes = (Set<Attribute<?, ?>>) type.getAttributes();
      for (Attribute<?, ?> attribute : attributes) {
        if(jpqlQuery.contains(attribute.getName()) &&
          attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED) {
          return true;
        }
      }
    }
  }
  return false;
}
 
Example 2
Source File: JpaUtils.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * Get all attributes where type or element type is assignable from class and has persistent type
 * @param type entity type
 * @param persistentType persistentType
 * @param clazz class
 * @return Set with matching attributes
 */
public static Set<Attribute<?, ?>> getAttributes(EntityType<?> type, PersistentAttributeType persistentType, 
		Class<?> clazz) {
	Set<Attribute<?, ?>> attributes = new HashSet<Attribute<?, ?>>();
	
	for (Attribute<?, ?> a : type.getAttributes()) {
		if (a.getPersistentAttributeType() == persistentType && isTypeOrElementType(a, clazz)) {
			attributes.add(a);
		}
	}
	
	return attributes;
}
 
Example 3
Source File: GraphQLSchemaBuilder.java    From graphql-jpa with MIT License 4 votes vote down vote up
private boolean isValidInput(Attribute attribute) {
    return attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.BASIC ||
            attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ELEMENT_COLLECTION ||
            attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED;
}
 
Example 4
Source File: JpaDao.java    From jdal with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
					}
				}
			}
		}
	}
}