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

The following examples show how to use javax.persistence.metamodel.Attribute#getName() . 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: JPAModelStructureBuilder.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Add an attribute to the model
 *
 * @param attr           the attribute
 * @param dataMartEntity the parent entity
 */
private void addField(Attribute attr, IModelEntity dataMartEntity, String keyPrefix) {
	String n = attr.getName();
	Member m = attr.getJavaMember();
	Class c = attr.getJavaType();
	String type = c.getName();

	// TODO: SCALE E PREC
	int scale = 0;
	int precision = 0;

	IModelField modelField = dataMartEntity.addNormalField(keyPrefix + attr.getName());
	modelField.setType(type);
	modelField.setPrecision(precision);
	modelField.setLength(scale);
	modelField.setJavaClass(c);
	propertiesInitializer.addProperties(modelField);
}
 
Example 2
Source File: JPAEdmProperty.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private String getReferenceColumnName(AnnotatedElement annotatedElement2, Attribute<?, ?> referencedAttribute) {
  String refColName = null;
  Column c = annotatedElement2.getAnnotation(Column.class);
  if(c != null) {
    refColName = c.name();
  }
  return refColName == null || "".equals(refColName)
      ? referencedAttribute.getName()
      : refColName;
}
 
Example 3
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;
}