Search code examples
hibernatejpapersistencewildfly-10

How to check how many entities are attached to my persistence context?


I'm currently running into some memory issues and looking at my entity model and my services. There are parts with lots of Eager Loading. There are parts with lots of JPA Collections. There are algorithms that traverse a huge tree in my domain model.

At the end of a transaction, I'd really like to know:

How many JPA entities did I load into storage during this transaction?

Is there any way to figure that out? Some persistence context cache size maybe? Somethine like this?:

em.countEntities(Customer.class)

Solution

  • Well, assuming you are using Hibernate as ORM you can use org.hibernate.stat.Statistics to check both first and second level cache infos. Check all available methods here.