List of usage examples for javax.persistence.metamodel ManagedType getAttribute
Attribute<? super X, ?> getAttribute(String name);
From source file:com.dbs.sdwt.jpa.ByExampleUtil.java
public <T> List<Predicate> byExampleOnXToMany(ManagedType<T> mt, Root<T> mtPath, T mtValue, SearchParameters sp, CriteriaBuilder builder) {//w w w . j ava 2 s .co m List<Predicate> predicates = newArrayList(); for (PluralAttribute<? super T, ?, ?> pa : mt.getPluralAttributes()) { if (pa.getCollectionType() == PluralAttribute.CollectionType.LIST) { List<?> values = (List<?>) jpaUtil.getValue(mtValue, mt.getAttribute(pa.getName())); if (values != null && !values.isEmpty()) { if (sp.getUseAndInXToMany()) { if (values.size() > 3) { log.warn( "Please note that using AND restriction on an Many to Many relationship requires as many joins as values"); } for (Object value : values) { ListJoin<T, ?> join = mtPath.join(mt.getList(pa.getName())); predicates.add(join.in(value)); } } else { ListJoin<T, ?> join = mtPath.join(mt.getList(pa.getName())); predicates.add(join.in(values)); } } } } return predicates; }
From source file:com.dbs.sdwt.jpa.ByExampleUtil.java
@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)); }/*w w w .j a v a 2 s. c om*/ } } } return predicates; }
From source file:net.sf.gazpachoquest.qbe.ByExampleSpecification.java
public <T extends Persistable> Specification<T> byExampleOnEntity(final T example, final SearchParameters sp) { Validate.notNull(example, "example must not be null"); return new Specification<T>() { @Override//w ww . j a v a2 s . co m public Predicate toPredicate(final Root<T> rootPath, final CriteriaQuery<?> query, final CriteriaBuilder builder) { Class<T> type = rootPath.getModel().getBindableJavaType(); ManagedType<T> mt = em.getMetamodel().entity(type); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.addAll(byExample(mt, rootPath, example, sp, builder)); predicates.addAll(byExampleOnXToOne(mt, rootPath, example, sp, builder)); // 1 level deep only predicates.addAll(byExampleOnManyToMany(mt, rootPath, example, sp, builder)); // order by query.orderBy(JpaUtil.buildJpaOrders(sp.getOrders(), rootPath, builder)); return JpaUtil.andPredicate(builder, predicates); } //https://github.com/jaxio/generated-projects/tree/master/jsf2-spring-conversation/src/main/generated-java/com/jaxio/appli/repository/support public <T extends Persistable> List<Predicate> byExample(final ManagedType<T> mt, final Path<T> mtPath, final T mtValue, final SearchParameters sp, final CriteriaBuilder builder) { List<Predicate> predicates = new ArrayList<Predicate>(); for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) { if (attr.getPersistentAttributeType() == MANY_TO_ONE || attr.getPersistentAttributeType() == ONE_TO_ONE || attr.getPersistentAttributeType() == EMBEDDED || attr.getJavaType().isAssignableFrom(Map.class)) { continue; } Object attrValue = getValue(mtValue, attr); if (attrValue != null) { if (attr.getJavaType() == String.class) { if (isNotEmpty((String) attrValue)) { SingularAttribute<? super T, String> stringAttribute = mt .getSingularAttribute(attr.getName(), String.class); predicates.add(JpaUtil.stringPredicate(mtPath.get(stringAttribute), attrValue, sp, builder)); } } else { SingularAttribute<? super T, ?> attribute = mt.getSingularAttribute(attr.getName(), attr.getJavaType()); // apply equal predicates.add(builder.equal(mtPath.get(attribute), attrValue)); } } } return predicates; } /** * Construct a join predicate on collection (eg many to many, List) */ public <T extends Persistable> List<Predicate> byExampleOnManyToMany(final ManagedType<T> mt, final Root<T> mtPath, final T mtValue, final SearchParameters sp, final CriteriaBuilder builder) { List<Predicate> predicates = new ArrayList<Predicate>(); for (PluralAttribute<T, ?, ?> pa : mt.getDeclaredPluralAttributes()) { if (pa.getCollectionType() == PluralAttribute.CollectionType.LIST) { List<?> value = (List<?>) getValue(mtValue, mt.getAttribute(pa.getName())); if (value != null && !value.isEmpty()) { ListJoin<T, ?> join = mtPath.join(mt.getDeclaredList(pa.getName())); predicates.add(join.in(value)); } } if (pa.getCollectionType() == PluralAttribute.CollectionType.SET) { Set<?> value = (Set<?>) getValue(mtValue, mt.getAttribute(pa.getName())); if (value != null && !value.isEmpty()) { SetJoin<T, ?> join = mtPath.join(mt.getDeclaredSet(pa.getName())); predicates.add(join.in(value)); } } } return predicates; } /** * Invoke byExample method for each not null x-to-one association * when their pk is not set. This allows you * to search entities based on an associated entity's properties * value. */ @SuppressWarnings("unchecked") public <T extends Persistable, M2O extends Persistable> List<Predicate> byExampleOnXToOne( final ManagedType<T> mt, final Root<T> mtPath, final T mtValue, final SearchParameters sp, final CriteriaBuilder builder) { List<Predicate> predicates = new ArrayList<Predicate>(); for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) { if (attr.getPersistentAttributeType() == MANY_TO_ONE || attr.getPersistentAttributeType() == ONE_TO_ONE) { // M2O m2oValue = (M2O) getValue(mtValue, mt.getAttribute(attr.getName())); if (m2oValue != null) { Class<M2O> m2oType = (Class<M2O>) attr.getBindableJavaType(); ManagedType<M2O> m2oMt = em.getMetamodel().entity(m2oType); Path<M2O> m2oPath = (Path<M2O>) mtPath.get(attr); predicates.addAll(byExample(m2oMt, m2oPath, m2oValue, sp, builder)); } } } return predicates; } private <T> Object getValue(final T example, final Attribute<? super T, ?> attr) { Object value = null; try { if (attr.getJavaMember() instanceof Method) { value = ((Method) attr.getJavaMember()).invoke(example, new Object[0]); } else if (attr.getJavaMember() instanceof Field) { value = ReflectionUtils.getField((Field) attr.getJavaMember(), example); } if (value instanceof ValueHolderInterface) { value = ((ValueHolderInterface) value).getValue(); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } return value; } }; }
From source file:org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder.java
@SuppressWarnings({ "rawtypes", "unchecked", "serial" }) public Path getPath(From root, FieldPath fieldPath, final CriteriaBuilder builder) { FieldPath myFieldPath = fieldPath;//from w ww . j a va 2s .c o m if (!StringUtils.isEmpty(fieldPath.getTargetProperty())) { myFieldPath = getFieldPath(root, fieldPath.getTargetProperty()); } From myRoot = root; for (String pathElement : myFieldPath.getAssociationPath()) { myRoot = myRoot.join(pathElement); } Path path = myRoot; for (int i = 0; i < myFieldPath.getTargetPropertyPieces().size(); i++) { String piece = myFieldPath.getTargetPropertyPieces().get(i); if (path.getJavaType().isAnnotationPresent(Embeddable.class)) { String original = ((SingularAttributePath) path).getAttribute().getDeclaringType().getJavaType() .getName() + "." + ((SingularAttributePath) path).getAttribute().getName() + "." + piece; String copy = path.getJavaType().getName() + "." + piece; copyCollectionPersister(original, copy, ((CriteriaBuilderImpl) builder).getEntityManagerFactory().getSessionFactory()); } try { path = path.get(piece); } catch (IllegalArgumentException e) { // We weren't able to resolve the requested piece, likely because it's in a polymoprhic version // of the path we're currently on. Let's see if there's any polymoprhic version of our class to // use instead. EntityManagerFactoryImpl em = ((CriteriaBuilderImpl) builder).getEntityManagerFactory(); Metamodel mm = em.getMetamodel(); boolean found = false; Class<?>[] polyClasses = dynamicDaoHelper.getAllPolymorphicEntitiesFromCeiling(path.getJavaType(), em.getSessionFactory(), true, true); for (Class<?> clazz : polyClasses) { ManagedType mt = mm.managedType(clazz); try { Attribute attr = mt.getAttribute(piece); if (attr != null) { Root additionalRoot = criteria.from(clazz); restrictions.add(builder.equal(path, additionalRoot)); path = additionalRoot.get(piece); found = true; break; } } catch (IllegalArgumentException e2) { // Do nothing - we'll try the next class and see if it has the attribute } } if (!found) { throw new IllegalArgumentException( "Could not resolve requested attribute against path, including" + " known polymorphic versions of the root", e); } } if (path.getParentPath() != null && path.getParentPath().getJavaType().isAnnotationPresent(Embeddable.class) && path instanceof PluralAttributePath) { //We need a workaround for this problem until it is resolved in Hibernate (loosely related to and likely resolved by https://hibernate.atlassian.net/browse/HHH-8802) //We'll throw a specialized exception (and handle in an alternate flow for calls from BasicPersistenceModule) throw new CriteriaConversionException(String.format( "Unable to create a JPA criteria Path through an @Embeddable object to a collection that resides therein (%s)", fieldPath.getTargetProperty()), fieldPath); // //TODO this code should work, but there still appear to be bugs in Hibernate's JPA criteria handling for lists // //inside Embeddables // Class<?> myClass = ((PluralAttributePath) path).getAttribute().getClass().getInterfaces()[0]; // //we don't know which version of "join" to call, so we'll let reflection figure it out // try { // From embeddedJoin = myRoot.join(((SingularAttributePath) path.getParentPath()).getAttribute()); // Method join = embeddedJoin.getClass().getMethod("join", myClass); // path = (Path) join.invoke(embeddedJoin, ((PluralAttributePath) path).getAttribute()); // } catch (Exception e) { // throw new RuntimeException(e); // } } } return path; }
From source file:org.querybyexample.jpa.ByExampleUtil.java
/** * Invoke byExample method for each not null x-to-one association when their pk is not set. This allows you to search entities based on an associated * entity's properties value.// w ww.j a v a 2 s. c o m */ @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; }
From source file:org.querybyexample.jpa.ByExampleUtil.java
/** * Construct a join predicate on collection (eg many to many, List) *//*from w w w . j a v a2 s. co m*/ public <T> List<Predicate> byExampleOnManyToMany(ManagedType<T> mt, Root<T> mtPath, T mtValue, SearchParameters sp, CriteriaBuilder builder) { List<Predicate> predicates = newArrayList(); for (PluralAttribute<? super T, ?, ?> pa : mt.getPluralAttributes()) { if (pa.getCollectionType() == PluralAttribute.CollectionType.LIST) { List<?> values = (List<?>) JpaUtil.getValue(mtValue, mt.getAttribute(pa.getName())); if (values != null && !values.isEmpty()) { if (sp.getUseANDInManyToMany()) { if (values.size() > 3) { log.warn( "Please note that using AND restriction on an Many to Many relationship requires as many joins as values"); } for (Object value : values) { ListJoin<T, ?> join = mtPath.join(mt.getList(pa.getName())); predicates.add(join.in(value)); } } else { ListJoin<T, ?> join = mtPath.join(mt.getList(pa.getName())); predicates.add(join.in(values)); } } } } return predicates; }