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

The following examples show how to use javax.persistence.metamodel.SingularAttribute#getPersistentAttributeType() . 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: SingularAttributeJoin.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked" })
public SingularAttributeJoin(
		CriteriaBuilderImpl criteriaBuilder,
		Class<X> javaType,
		PathSource<O> pathSource,
		SingularAttribute<? super O, ?> joinAttribute,
		JoinType joinType) {
	super( criteriaBuilder, javaType, pathSource, joinAttribute, joinType );
	if ( Attribute.PersistentAttributeType.EMBEDDED == joinAttribute.getPersistentAttributeType() ) {
		this.model = (Bindable<X>) joinAttribute;
	}
	else {
		if ( javaType != null ) {
			this.model = (Bindable<X>) criteriaBuilder.getEntityManagerFactory().getMetamodel().managedType( javaType );
		}
		else {
			this.model = (Bindable<X>) joinAttribute.getType();
		}
	}
}
 
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: 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 5
Source File: SingularAttributePath.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private ManagedType<X> resolveManagedType(SingularAttribute<?, X> attribute) {
	if ( Attribute.PersistentAttributeType.BASIC == attribute.getPersistentAttributeType() ) {
		return null;
	}
	else if ( Attribute.PersistentAttributeType.EMBEDDED == attribute.getPersistentAttributeType() ) {
		return (EmbeddableType<X>) attribute.getType();
	}
	else {
		return (IdentifiableType<X>) attribute.getType();
	}
}
 
Example 6
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);
	}
}