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

The following examples show how to use javax.persistence.metamodel.Attribute#getJavaType() . 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: JpaUtil.java    From javaee-lab with Apache License 2.0 6 votes vote down vote up
public void verifyPath(List<Attribute<?, ?>> path) {
    List<Attribute<?, ?>> attributes = newArrayList(path);
    Class<?> from = null;
    if (attributes.get(0).isCollection()) {
        from = ((PluralAttribute) attributes.get(0)).getElementType().getJavaType();
    } else {
        from = attributes.get(0).getJavaType();
    }
    attributes.remove(0);
    for (Attribute<?, ?> attribute : attributes) {
        if (!attribute.getDeclaringType().getJavaType().isAssignableFrom(from)) {
            throw new IllegalStateException("Wrong path.");
        }
        from = attribute.getJavaType();
    }
}
 
Example 3
Source File: MetamodelUtil.java    From javaee-lab with Apache License 2.0 6 votes vote down vote up
public List<Attribute<?, ?>> toAttributes(String path, Class<?> from) {
    try {
        List<Attribute<?, ?>> attributes = newArrayList();
        Class<?> current = from;
        for (String pathItem : Splitter.on(".").split(path)) {
            Class<?> metamodelClass = getCachedClass(current);
            Field field = metamodelClass.getField(pathItem);
            Attribute<?, ?> attribute = (Attribute<?, ?>) field.get(null);
            attributes.add(attribute);
            if (attribute instanceof PluralAttribute) {
                current = ((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType();
            } else {
                current = attribute.getJavaType();
            }
        }
        return attributes;
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example 4
Source File: AbstractJoinImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public AbstractJoinImpl(
		CriteriaBuilderImpl criteriaBuilder,
		PathSource<Z> pathSource,
		Attribute<? super Z, X> joinAttribute,
		JoinType joinType) {
	this( criteriaBuilder, joinAttribute.getJavaType(), pathSource, joinAttribute, joinType );
}
 
Example 5
Source File: JpaDataFetcher.java    From graphql-jpa with MIT License 5 votes vote down vote up
private Class getJavaType(DataFetchingEnvironment environment, Argument argument) {
    Attribute argumentEntityAttribute = getAttribute(environment, argument);

    if (argumentEntityAttribute instanceof PluralAttribute)
        return ((PluralAttribute) argumentEntityAttribute).getElementType().getJavaType();

    return argumentEntityAttribute.getJavaType();
}
 
Example 6
Source File: PropertySelector.java    From gazpachoquest with GNU General Public License v3.0 5 votes vote down vote up
private void verifyPath(List<Attribute<?, ?>> attributes) {
    Class<?> from = attributes.get(0).getJavaType();
    attributes.remove(0);
    for (Attribute<?, ?> attribute : attributes) {
        if (!attribute.getDeclaringType().getJavaType().isAssignableFrom(from)) {
            throw new IllegalStateException("Wrong path.");
        }
        from = attribute.getJavaType();
    }
}
 
Example 7
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 8
Source File: HibernateCriteriaBuilder.java    From gorm-hibernate5 with Apache License 2.0 4 votes vote down vote up
protected Class getClassForAssociationType(Attribute<?, ?> type) {
    if (type instanceof PluralAttribute) {
        return ((PluralAttribute)type).getElementType().getJavaType();
    }
    return type.getJavaType();
}