protected void clearCollections(EntityManager em, Class<?>... entityClasses) { for (Class<?> c : entityClasses) { List<String> deletes = PLURAL_DELETES.get(c); if (deletes == null) { Metamodel entityMetamodel = cbf.getService(Metamodel.class); EntityType<?> t = entityMetamodel.entity(c); deletes = new ArrayList<>(); for (PluralAttribute<?, ?, ?> pluralAttribute : t.getPluralAttributes()) { JoinTable joinTable = jpaProvider.getJoinTable(t, pluralAttribute.getName()); if (joinTable != null) { deletes.add("delete from " + joinTable.getTableName()); } } PLURAL_DELETES.put(c, deletes); } for (String delete : deletes) { em.createNativeQuery(delete).executeUpdate(); } } }
protected void clearCollections(EntityManager em, Class<?>... entityClasses) { for (Class<?> c : entityClasses) { List<String> deletes = PLURAL_DELETES.get(c); if (deletes == null) { Metamodel entityMetamodel = cbf.getService(Metamodel.class); EntityType<?> t = entityMetamodel.entity(c); deletes = new ArrayList<>(); for (PluralAttribute<?, ?, ?> pluralAttribute : t.getPluralAttributes()) { JoinTable joinTable = jpaProvider.getJoinTable(t, pluralAttribute.getName()); if (joinTable != null) { deletes.add("delete from " + joinTable.getTableName()); } } PLURAL_DELETES.put(c, deletes); } for (String delete : deletes) { em.createNativeQuery(delete).executeUpdate(); } } }
@SuppressWarnings("unchecked") // Unfortunately, @SafeVarargs cannot be used as it requires a final method. private E fetchPluralAttributes(E entity, java.util.function.Predicate<CollectionType> ofType, Function<E, ?>... getters) { if (isEmpty(getters)) { for (PluralAttribute<?, ?, ?> a : getMetamodel().getPluralAttributes()) { if (ofType.test(a.getCollectionType())) { ofNullable(invokeGetter(entity, a.getName())).ifPresent(c -> invokeMethod(c, "size")); } } } else { stream(getters).forEach(getter -> ofNullable(getter.apply(entity)).ifPresent(c -> invokeMethod(c, "size"))); } return entity; }
/** * This clumsy code is just to get the class of plural attribute mapping * * @param et * @param fieldName * @return */ private Class<?> getPluralJavaType(EntityType<?> et, String fieldName) { for (PluralAttribute<?,?,?> pa : et.getPluralAttributes()) { if (pa.getName().equals(fieldName)) { switch (pa.getCollectionType()) { case COLLECTION: return et.getCollection(fieldName).getElementType().getJavaType(); case LIST: return et.getList(fieldName).getElementType().getJavaType(); case SET: return et.getSet(fieldName).getElementType().getJavaType(); case MAP: throw new UnsupportedOperationException("Entity Map mapping unsupported for entity: " + et.getName() + " field name: " + fieldName); } } } throw new IllegalArgumentException("Field " + fieldName + " of entity " + et.getName() + " is not a plural attribute"); }
for (PluralAttribute<?,?,?> plural : entityType.getPluralAttributes()) { JPACollectionType jpaCollection = new JPACollectionType(plural, this.secureEntity); String collectionName = StringUtilities.singularize(jpaCollection.getName());
metadata.setCollections(getCollectionsFromMetadata((Set) entityType.getPluralAttributes())); } catch (RuntimeException ex) { LOG.error("Error processing collection metadata for " + entityType.getBindableJavaType().getName());
PluralAttribute<?, ?, ?> attr = Enumerable.create( context.getEntity().getJPAEntityType() .getPluralAttributes()) .firstOrNull(new Predicate1() { public boolean apply(Object input) {
cmm.append(Stmt.loadVariable("entityType").invoke("addAttribute", attribSnapshot)); for (PluralAttribute<?, ?, ?> attrib : et.getPluralAttributes()) { Statement attribSnapshot = SnapshotMaker.makeSnapshotAsSubclass( attrib, PluralAttribute.class, ErraiPluralAttribute.class, methodBodyCallback,