Reputation: 4577
In the raw JPA specification (so not a specific implementation like Hibernate or EclipseLink), is there an option to call em.clear()
for a specific type of entity but not for others?
The reason I'm asking is, I have a process which creates and stores tens of thousands of entities; but each is not needed again by the entity manager after it's been persisted. However, each entity has a child entity which is one of about 30 records in the database. So I want to be able to call something like em.clear(MainEntity.class)
, meaning I don't clear the cache of the child entity which is reused, but also don't build up a history of tens of thousands of unneeded records.
Here's a simplified example of what I am currently doing:
List<Child> children = (List<Child>)em.createQuery("Select c from " + Child.entityName + " c").getResultList();
for (int i=0;i<700000;i++)
{
Parent p = createParent(i, getRandomChild(children));
em.persist(p);
if (i%1000 == 0)
{
em.flush(); // push to db
em.clear(); // remove old items from cache - but unfortunately removes children too
// as I've cleared the cache, I need to repopulate this to reattach the children again
children = (List<Child>)em.createQuery("Select c from " + Child.entityName + " c").getResultList();
}
}
Upvotes: 0
Views: 634
Reputation: 111
You can use
void detach(java.lang.Object entity)
like this
em.detach(p);
to remove only parent from the persistence context.
Upvotes: 1