javax.persistence.metamodel.Attribute.PersistentAttributeType Java Examples

The following examples show how to use javax.persistence.metamodel.Attribute.PersistentAttributeType. 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: JPAEdmPropertyTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public PersistentAttributeType getPersistentAttributeType() {
  if (attributeName.equals("SOLITID")) {
    return PersistentAttributeType.BASIC;
  }
  return ATTRIBUTE_TYPE;
}
 
Example #2
Source File: JPAEdmPropertyTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
private void setValuesToSet() {
  if (JPAEdmPropertyTest.ATTRIBUTE_TYPE.equals(PersistentAttributeType.BASIC)) {
    attributeSet.add((Attribute<? super String, String>) new JPAEdmAttribute(java.lang.String.class, "SOID"));
    attributeSet.add((Attribute<? super String, String>) new JPAEdmAttribute(java.lang.String.class, "SONAME"));
  } else if (JPAEdmPropertyTest.ATTRIBUTE_TYPE.equals(PersistentAttributeType.EMBEDDED)) {
    attributeSet.add(new JPAEdmAttribute(JPAEdmEmbeddable.class, ComplexType.ComplexTypeA.clazz.getName()));
  } else if (JPAEdmPropertyTest.ATTRIBUTE_TYPE.equals(PersistentAttributeType.MANY_TO_ONE)) {
    attributeSet.add(new JPAEdmPluralAttribute());
  }
}
 
Example #3
Source File: JPAEdmPropertyTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public Member getJavaMember() {
  if (ATTRIBUTE_TYPE.equals(PersistentAttributeType.MANY_TO_ONE)) {
    return new JPAJavaMember();
  }
  return null;

}
 
Example #4
Source File: JPAEdmPropertyTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Class<String> getJavaType() {
  Class<?> clazz = null;
  if (ATTRIBUTE_TYPE.equals(PersistentAttributeType.BASIC)) {
    clazz = (Class<java.lang.String>) SimpleType.SimpleTypeA.clazz;
  } else {
    clazz = (Class<?>) ComplexType.ComplexTypeA.clazz;
  }
  return (Class<String>) clazz;
}
 
Example #5
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 #6
Source File: RSQLVisitorBase.java    From rsql-jpa-specification with MIT License 4 votes vote down vote up
protected <T> boolean isEmbeddedType(String property, ManagedType<T> classMetadata) {
	return classMetadata.getAttribute(property).getPersistentAttributeType() == PersistentAttributeType.EMBEDDED;
}
 
Example #7
Source File: RSQLVisitorBase.java    From rsql-jpa-specification with MIT License 4 votes vote down vote up
protected <T> boolean isElementCollectionType(String property, ManagedType<T> classMetadata) {
	return classMetadata.getAttribute(property).getPersistentAttributeType() == PersistentAttributeType.ELEMENT_COLLECTION;
}
 
Example #8
Source File: JPAModelStructureBuilder.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * This method adds the normal fields to the model entry structure
 *
 * @param modelEntity: the model entity to complete adding normal fields
 *
 * @return a list of entities in ONE_TO_MANY relationship with the entity passed in as parameter (i.e. entities whose input entity is related to by means of
 *         e foreign key - MANY_TO_ONE relatioship)
 */
public List<IModelEntity> addNormalFields(IModelEntity modelEntity) {

	logger.debug("Adding the field " + modelEntity.getName());
	List<IModelEntity> subEntities = new ArrayList<IModelEntity>();
	EntityType thisEntityType = null;

	Metamodel classMetadata = getEntityManager().getMetamodel();

	for (Iterator it = classMetadata.getEntities().iterator(); it.hasNext();) {
		EntityType et = (EntityType) it.next();
		if (et.getJavaType().getName().equals(modelEntity.getType())) {
			thisEntityType = et;
			break;
		}
	}

	if (thisEntityType == null) {
		return new ArrayList();
	}

	Set<Attribute> attributes = thisEntityType.getAttributes();
	Iterator<Attribute> attributesIt = attributes.iterator();

	while (attributesIt.hasNext()) {
		Attribute a = attributesIt.next();
		// normal attribute
		if (a.getPersistentAttributeType().equals(PersistentAttributeType.BASIC)) {
			addField(a, modelEntity, "");
		} else if (a.getPersistentAttributeType().equals(PersistentAttributeType.MANY_TO_ONE)) { // relation
			Class c = a.getJavaType();
			javax.persistence.JoinColumn joinColumn = null;
			String entityType = c.getName();
			String columnName = a.getName();
			String joinColumnnName = a.getName();
			String entityName = a.getName(); // getEntityNameFromEntityType(entityType);

			try {
				joinColumn = (((java.lang.reflect.Field) a.getJavaMember()).getAnnotation(javax.persistence.JoinColumn.class));
			} catch (Exception e) {
				logger.error("Error loading the join column annotation for entity " + entityName, e);
			}

			if (joinColumn != null) {
				joinColumnnName = joinColumn.name();
				// add in the entity a property that maps the column name with the join column
				modelEntity.getProperties().put(columnName, joinColumnnName);
			}

			IModelEntity subentity = new ModelEntity(entityName, null, columnName, entityType, modelEntity.getStructure());
			subEntities.add(subentity);
		} else if (a.getPersistentAttributeType().equals(PersistentAttributeType.EMBEDDED)) { // key
			Set<Attribute> keyAttre = ((EmbeddableType) ((SingularAttribute) a).getType()).getAttributes();
			Iterator<Attribute> keyIter = keyAttre.iterator();
			while (keyIter.hasNext()) {
				addField(keyIter.next(), modelEntity, a.getName() + ".");
			}
		}
	}

	logger.debug("Field " + modelEntity.getName() + " added");
	return subEntities;
}
 
Example #9
Source File: JPAEdmPropertyTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public javax.persistence.metamodel.Attribute.PersistentAttributeType getPersistentAttributeType() {
  return ATTRIBUTE_TYPE;
}
 
Example #10
Source File: JPAEdmPropertyTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public PersistentAttributeType getPersistentAttributeType() {
  return ATTRIBUTE_TYPE;
}
 
Example #11
Source File: EclipseLinkJpaMetadataProviderImpl.java    From rice with Educational Community License v2.0 4 votes vote down vote up
/**
    * {@inheritDoc}
    */
@Override
protected void populateImplementationSpecificRelationshipLevelMetadata(DataObjectRelationshipImpl relationship,
		SingularAttribute<?, ?> rd) {
	// We need to go into the repository and grab the table name.
	Class<?> referencedClass = rd.getBindableJavaType();
	EntityType<?> referencedEntityType = entityManager.getMetamodel().entity(referencedClass);
	if (referencedEntityType instanceof EntityTypeImpl) {
		relationship
				.setBackingObjectName(((EntityTypeImpl<?>) referencedEntityType).getDescriptor().getTableName());
	}
	// Set to read only if store (save) operations should not be pushed through
	PersistentAttributeType persistentAttributeType = rd.getPersistentAttributeType();

	if (rd instanceof SingularAttributeImpl) {
		SingularAttributeImpl<?, ?> rel = (SingularAttributeImpl<?, ?>) rd;

		OneToOneMapping relationshipMapping = (OneToOneMapping) rel.getMapping();
		relationship.setReadOnly(relationshipMapping.isReadOnly());
		relationship.setSavedWithParent(relationshipMapping.isCascadePersist());
		relationship.setDeletedWithParent(relationshipMapping.isCascadeRemove());
		relationship.setLoadedAtParentLoadTime(relationshipMapping.isCascadeRefresh()
				&& !relationshipMapping.isLazy());
		relationship.setLoadedDynamicallyUponUse(relationshipMapping.isCascadeRefresh()
				&& relationshipMapping.isLazy());

           List<DataObjectAttributeRelationship> attributeRelationships = new ArrayList<DataObjectAttributeRelationship>();
           List<String> referencedEntityPkFields = getPrimaryKeyAttributeNames(referencedEntityType);

           for (String referencedEntityPkField : referencedEntityPkFields) {
               for (Map.Entry<DatabaseField, DatabaseField> entry :
                       relationshipMapping.getTargetToSourceKeyFields().entrySet()) {
                   DatabaseField childDatabaseField = entry.getKey();
                   String childFieldName = getPropertyNameFromDatabaseColumnName(referencedEntityType,
                           childDatabaseField.getName());

                   if (referencedEntityPkField.equalsIgnoreCase(childFieldName)) {
                       DatabaseField parentDatabaseField = entry.getValue();
                       String parentFieldName = getPropertyNameFromDatabaseColumnName(rd.getDeclaringType(),
                               parentDatabaseField.getName());

                       if (parentFieldName != null) {
                           attributeRelationships
                                   .add(new DataObjectAttributeRelationshipImpl(parentFieldName, childFieldName));
                           break;
                       } else {
                           LOG.warn("Unable to find parent field reference.  There may be a JPA mapping problem on " +
                                   referencedEntityType.getJavaType() + ": " + relationship);
                       }
                   }
               }
           }

           relationship.setAttributeRelationships(attributeRelationships);

           populateInverseRelationship(relationshipMapping, relationship);

	} else {
		// get what we can based on JPA values (note that we just set some to have values here)
		relationship.setReadOnly(persistentAttributeType == PersistentAttributeType.MANY_TO_ONE);
		relationship.setSavedWithParent(persistentAttributeType == PersistentAttributeType.ONE_TO_ONE);
		relationship.setDeletedWithParent(persistentAttributeType == PersistentAttributeType.ONE_TO_ONE);
		relationship.setLoadedAtParentLoadTime(true);
		relationship.setLoadedDynamicallyUponUse(false);
	}
}
 
Example #12
Source File: JpaMetadataProviderImpl.java    From rice with Educational Community License v2.0 4 votes vote down vote up
/**
 * Extracts the collection metadata from a single JPA {@link PluralAttribute} object.
    *
    * @param cd The plural attribute to process.
    * @return The collection metadata from a single JPA {@link PluralAttribute} object.
 */
protected DataObjectCollection getCollectionMetadataFromCollectionAttribute(PluralAttribute cd) {
	try {
		DataObjectCollectionImpl collection = new DataObjectCollectionImpl();

		// OJB stores the related class object name. We need to go into the repository and grab the table name.
		Class<?> collectionElementClass = cd.getElementType().getJavaType();
		EntityType<?> elementEntityType = entityManager.getMetamodel().entity(collectionElementClass);
		collection.setName(cd.getName());
		collection.setRelatedType(collectionElementClass);
		populateImplementationSpecificCollectionLevelMetadata(collection, cd);

		// Set to read only if store (save) operations should not be pushed through
		PersistentAttributeType persistentAttributeType = cd.getPersistentAttributeType();
		
		// default case:  Without any mapping attributes, collections are linked by their primary key
		if (persistentAttributeType == PersistentAttributeType.ONE_TO_MANY) {
			// TODO: We probably still need to handle the "mappedBy" property on the OneToMany definition
			
			// We only perform this logic here if we did not populate it in the implementation-specific call above
			if (collection.getAttributeRelationships().isEmpty()) {
				// need to obtain the keys for the relationship
				List<String> pkFields = getPrimaryKeyAttributeNames((EntityType<?>) cd.getDeclaringType());
				List<String> fkFields = getPrimaryKeyAttributeNames(elementEntityType);
				List<DataObjectAttributeRelationship> attributeRelationships = new ArrayList<DataObjectAttributeRelationship>();
				for (int i = 0; i < pkFields.size(); i++) {
					attributeRelationships.add(new DataObjectAttributeRelationshipImpl(pkFields.get(i), fkFields
							.get(i)));
				}
				collection.setAttributeRelationships(attributeRelationships);
			}
		} else if ( persistentAttributeType == PersistentAttributeType.MANY_TO_MANY ) {
			// OK, this is an assumption
			collection.setIndirectCollection( true );
			// And, since the connection is set at the *database* level through the @JoinTable anotation
			// we do not have any field names with which to make the connection
			collection.setAttributeRelationships(null);
		}
		
		return collection;
	} catch (RuntimeException ex) {
		LOG.error("Unable to process Collection metadata: " + cd);
		throw ex;
	}
}
 
Example #13
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);
					}
				}
			}
		}
	}
}
 
Example #14
Source File: JpaUtils.java    From jdal with Apache License 2.0 2 votes vote down vote up
/**
 * Get all attributes of type by persistent type
 * @param type 
 * @param persistentType
 * @return a set with all attributes of type with persistent type persistentType.
 */
public static Set<Attribute<?, ?>> getAttributes(EntityType<?> type, PersistentAttributeType persistentType) {
	return getAttributes(type, persistentType, Object.class);
}