/** * 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 Identifiable<?>, M2O extends Identifiable<?>> List<Predicate> byExampleOnXToOne( ManagedType<T> mt, Root<T> mtPath, final 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; }
@Override public Map<String, Map<String, Class<?>>> getTableColumnMap() { Metamodel metamodel = entityManager.getEntityManagerFactory().getMetamodel(); Set<EntityType<?>> entityTypes = metamodel.getEntities(); Map<String, Map<String, Class<?>>> tableColumnMap = new HashMap<String, Map<String, Class<?>>>(); for (EntityType<?> type : entityTypes) { EntityType<Entity> entityType = (EntityType<Entity>) type; Set<SingularAttribute<? super Entity, ?>> singularAttributes = entityType.getSingularAttributes(); Map<String, Class<?>> columnMap = new HashMap<String, Class<?>>(); for (SingularAttribute<? super Entity, ?> singularAttribute : singularAttributes) { columnMap.put(singularAttribute.getName(), singularAttribute.getJavaType()); } tableColumnMap.put(entityType.getName(), columnMap); } return tableColumnMap; }
@SuppressWarnings("unchecked") private static String getEntityIdField(EntityManager em, Class entity) { String idProperty = ""; Metamodel metamodel = em.getMetamodel(); EntityType e = metamodel.entity(entity); Set<SingularAttribute> singularAttributes = e.getSingularAttributes(); for (SingularAttribute singularAttribute : singularAttributes) { if (singularAttribute.isId()) { idProperty = singularAttribute.getName(); break; } } return idProperty; }
@Override public String toString() { return attribute.getName() + " == " + value; }