V
V
Vitaly Stolyarov2017-01-13 03:43:32
Hibernate
Vitaly Stolyarov, 2017-01-13 03:43:32

How to remove all Embeddable objects from HashMap with specific key?

@Embeddable
class Item{
String value
}

class R{

@...
Map<Integer, Item> values;
}

The database has a list from R, each has an Embeddable collection with an integer key, so you need to remove all items from the Map using it. How to do it more efficiently?
The fastest, of course, is to execute a native query, but this will be "distance" from Hibernate, etc. etc.
Working but terrible method
List<R> r = JPA.em().createQuery("from R").getResultList();

        for (R resp : r) {

            resp.values.remove(f.getId());
            JPA.em().persist(resp);
        }

Fast and efficient, but minimal attention to Hibernate
Query qRemove = JPA.em().createNativeQuery("delete from r_values where val_id = :id");
        qRemove.setParameter("id", f.getId());
        qRemove.executeUpdate();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question