predicates 案例

private <T> List<T> findByExample(T example, Class<T> clazz)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException,
        NoSuchMethodException {
    EntityManager em = getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(clazz);
    Root<T> r = cq.from(clazz);
    Predicate p = cb.conjunction();
    Metamodel mm = em.getMetamodel();
    EntityType<T> et = mm.entity(clazz);
    Set<Attribute super T, ?>> attrs = et.getAttributes();
    for (Attribute super T, ?> a : attrs) {
        String name = a.getName();
        String javaName = a.getJavaMember().getName();
        String getter = "get" + javaName.substring(0, 1).toUpperCase() + javaName.substring(1);
        Method m = clazz.getMethod(getter, (Class[]) null);
        Object value = m.invoke(example, (Object[]) null);
        if (value != null) {
            if (!a.isCollection())
                p = cb.and(p, cb.equal(r.get(name), value));
            if (a.isAssociation())
                r.fetch(name);
        }
    }
    cq.select(r).where(p);
    TypedQuery<T> query = em.createQuery(cq);
    return query.getResultList();

}

@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;
}


private void addForeignKeys(MetadataFactory mf, Metamodel model, ManagedType entity, Table entityTable) throws TranslatorException {
	for (Attribute ?> attr:entity.getAttributes()) {
		if (attr.isCollection()) {
			
			PluralAttribute pa = (PluralAttribute)attr;
			Table forignTable = null;
			
			for (EntityType et:model.getEntities()) {
				if (et.getJavaType().equals(pa.getElementType().getJavaType())) {
					forignTable = mf.getSchema().getTable(et.getName());
					break;
				}
			}
			
			if (forignTable == null) {
				continue;
			}
			
			// add foreign keys as columns in table first; check if they exist first
			ArrayList<String> keys = new ArrayList<String>();
			KeyRecord pk = entityTable.getPrimaryKey();
			for (Column entityColumn:pk.getColumns()) {
				addColumn(mf, entityColumn.getName(), entityColumn.getDatatype().getRuntimeTypeName(), forignTable);
				keys.add(entityColumn.getName());
			}
			if (!foreignKeyExists(keys, forignTable)) {
				addForiegnKey(mf, attr.getName(), keys, entityTable.getName(), forignTable);
			}
		}
	}
}

你可能感兴趣的:(java,jsp,angularjs,spring)