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

The following examples show how to use javax.persistence.metamodel.SingularAttribute#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: AbstractPathImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings({ "unchecked" })
public <Y> Path<Y> get(SingularAttribute<? super X, Y> attribute) {
	if ( ! canBeDereferenced() ) {
		throw illegalDereference();
	}

	SingularAttributePath<Y> path = (SingularAttributePath<Y>) resolveCachedAttributePath( attribute.getName() );
	if ( path == null ) {
		path = new SingularAttributePath<Y>(
				criteriaBuilder(),
				attribute.getJavaType(),
				getPathSourceForSubPaths(),
				attribute
		);
		registerAttributePath( attribute.getName(), path );
	}
	return path;
}
 
Example 2
Source File: ByPatternUtil.java    From javaee-lab with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> Predicate byPattern(Root<T> root, CriteriaBuilder builder, SearchParameters sp, Class<T> type) {
    if (!sp.hasSearchPattern()) {
        return null;
    }

    List<Predicate> predicates = newArrayList();
    EntityType<T> entity = em.getMetamodel().entity(type);
    String pattern = sp.getSearchPattern();

    for (SingularAttribute<? super T, ?> attr : entity.getSingularAttributes()) {
        if (attr.getPersistentAttributeType() == MANY_TO_ONE || attr.getPersistentAttributeType() == ONE_TO_ONE) {
            continue;
        }

        if (attr.getJavaType() == String.class) {
            predicates.add(jpaUtil.stringPredicate((Expression<String>) root.get(jpaUtil.attribute(entity, attr)), pattern, sp, builder));
        }
    }

    return jpaUtil.orPredicate(builder, predicates);
}
 
Example 3
Source File: ByExampleUtil.java    From javaee-lab with Apache License 2.0 6 votes vote down vote up
public <T> List<Predicate> byExample(ManagedType<T> mt, Path<T> mtPath, T mtValue, SearchParameters sp, CriteriaBuilder builder) {
    List<Predicate> predicates = newArrayList();
    for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) {
        if (attr.getPersistentAttributeType() == MANY_TO_ONE //
                || attr.getPersistentAttributeType() == ONE_TO_ONE //
                || attr.getPersistentAttributeType() == EMBEDDED) {
            continue;
        }

        Object attrValue = jpaUtil.getValue(mtValue, attr);
        if (attrValue != null) {
            if (attr.getJavaType() == String.class) {
                if (isNotEmpty((String) attrValue)) {
                    predicates.add(jpaUtil.stringPredicate(mtPath.get(jpaUtil.stringAttribute(mt, attr)), attrValue, sp, builder));
                }
            } else {
                predicates.add(builder.equal(mtPath.get(jpaUtil.attribute(mt, attr)), attrValue));
            }
        }
    }
    return predicates;
}
 
Example 4
Source File: JpaMetadataProviderImpl.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
 * Gets a single field's metadata from the property descriptor.
 * 
 * @param persistableClass The class of the data object.
 * @param attr The singular attribute to process.
    * @param primaryKeyAttributes The list of primary key attribute names.
 * @return The DataObjectAttribute containing the metadata for the given attribute on the provided Class
 */
protected DataObjectAttribute getAttributeMetadata(Class<?> persistableClass, SingularAttribute<?, ?> attr,
		List<String> primaryKeyAttributes) {
	DataObjectAttributeImpl attribute = new DataObjectAttributeImpl();

	attribute.setOwningType(persistableClass);
	attribute.setName(attr.getName());
	Class<?> propertyType = attr.getJavaType();
	attribute.setType(propertyType);
	DataType dataType = DataType.getDataTypeFromClass(propertyType);
	if (dataType == null) {
		dataType = DataType.STRING;
	}
	attribute.setDataType(dataType);
	attribute.setRequired(!attr.isOptional() && !attr.isId() && !primaryKeyAttributes.contains(attr.getName()));

	populateImplementationSpecificAttributeLevelMetadata(attribute, attr);

	return attribute;
}