Java Code Examples for javax.persistence.metamodel.ManagedType#getSingularAttributes()

The following examples show how to use javax.persistence.metamodel.ManagedType#getSingularAttributes() . 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: 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 2
Source File: ByExampleUtil.java    From javaee-lab with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Identifiable<?>, M2O extends Identifiable<?>> List<Predicate> byExampleOnXToOne(ManagedType<T> mt, Root<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) {
            M2O m2oValue = (M2O) jpaUtil.getValue(mtValue, mt.getAttribute(attr.getName()));
            Class<M2O> m2oType = (Class<M2O>) attr.getBindableJavaType();
            Path<M2O> m2oPath = (Path<M2O>) mtPath.get(attr);
            ManagedType<M2O> m2oMt = em.getMetamodel().entity(m2oType);
            if (m2oValue != null) {
                if (m2oValue.isIdSet()) { // we have an id, let's restrict only on this field
                    predicates.add(builder.equal(m2oPath.get("id"), m2oValue.getId()));
                } else {
                    predicates.addAll(byExample(m2oMt, m2oPath, m2oValue, sp, builder));
                }
            }
        }
    }
    return predicates;
}
 
Example 3
Source File: ByFullTextUtil.java    From javaee-lab with Apache License 2.0 5 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 (!isPrimaryKey(mt, attr)) {
            continue;
        }

        Object attrValue = jpaUtil.getValue(mtValue, attr);
        if (attrValue != null) {
            predicates.add(builder.equal(mtPath.get(jpaUtil.attribute(mt, attr)), attrValue));
        }
    }
    return predicates;
}
 
Example 4
Source File: EclipseLinkJpaMetadataProviderImpl.java    From rice with Educational Community License v2.0 3 votes vote down vote up
/**
    * Returns the property name on the given entity type which the given database column is mapped to.
    *
    * <p>
    * If no field on the given type is mapped to this field (which is common in cases of a JPA relationship without an
    * actual {@link javax.persistence.Column} annotated field to represent the foreign key) then this method will
    * return null.
    * </p>
    *
    * @param entityType the entity type on which to search for a property that is mapped to the given column
    * @param databaseColumnName the name of the database column
    *
    * @return the name of the property on the given entity type which maps to the given column, or null if no such
    *         mapping exists
    */
@SuppressWarnings({ "unchecked", "rawtypes" })
   protected String getPropertyNameFromDatabaseColumnName(ManagedType entityType, String databaseColumnName) {
	for (SingularAttributeImpl attr : (Set<SingularAttributeImpl>) entityType.getSingularAttributes()) {
		if (!attr.isAssociation()) {
			if (!(attr.getClass().isAssignableFrom(EmbeddableTypeImpl.class)) &&
                       !(attr.getMapping().getClass().isAssignableFrom(AggregateObjectMapping.class)) &&
                       attr.getMapping().getField().getName().equals(databaseColumnName)) {
				return attr.getName();
			}
		}
	}
	return null;
}