public static void deleteAllExcept(EntityManager entityManager, Iterable<Class> entityClasses, Object... excludedEntities) {
        if (excludedEntities.length == 0) {
            deleteAll(entityManager, entityClasses);
        }
        ImmutableListMultimap<Class, Object> exclusion = Multimaps.index(ImmutableSet.copyOf(excludedEntities), new Function<Object, Class>() {
            @Override
            public Class apply(Object input) {
                return input.getClass();
            }
        });

        for (Class entityType : entityClasses) {
            EntityClass entityClass = EntityClass.from(entityType);
            Iterable<String> manyToManyTables = getAssociationTables(entityClass);

            Settable idSettable = Iterables.find(entityClass.getElements(), HasAnnotationPredicate.has(Id.class));
            List<Serializable> ids = getIds(exclusion.get(entityType), idSettable);

            for (String table : manyToManyTables) {
                // TODO need to consider exclusion as well
                deleteTable(entityManager, table);
            }
            // TODO element collection
            deleteEntityExcept(entityManager, entityType.getSimpleName(), exclusion.get(entityType), idSettable, ids);
        }

    }
public static void deleteAllExcept ( EntityManager entityManager , Iterable < Class > entityClasses , Object ... excludedEntities ) { if ( excludedEntities . length == 0 ) { deleteAll ( entityManager , entityClasses ) ; } ImmutableListMultimap < Class , Object > exclusion = Multimaps . index ( ImmutableSet . copyOf ( excludedEntities ) , new Function < Object , Class > ( ) { @ Override public Class apply ( Object input ) { return input . getClass ( ) ; } } ) ; for ( Class entityType : entityClasses ) { EntityClass entityClass = EntityClass . from ( entityType ) ; Iterable < String > manyToManyTables = getAssociationTables ( entityClass ) ; Settable idSettable = Iterables . find ( entityClass . getElements ( ) , HasAnnotationPredicate . has ( Id . class ) ) ; List < Serializable > ids = getIds ( exclusion . get ( entityType ) , idSettable ) ; for ( String table : manyToManyTables ) { deleteTable ( entityManager , table ) ; } deleteEntityExcept ( entityManager , entityType . getSimpleName ( ) , exclusion . get ( entityType ) , idSettable , ids ) ; } }
Delete all records from given entity representing tables except exclusion. Exclusion are given as entity object.
So a match on id will be used.
<p>
TODO manyToMany and element collection table do not consider exlusion yet

@param entityManager
entity manager
@param entityClasses
entity cless in correct order
@param excludedEntities
excluded persisted entity objects