JPA does not indicate such features, however you can do this with internal EclipseLink elements, for example:
L1 (transactional cache, persistence context)
import org.eclipse.persistence.jpa.JpaEntityManager;
import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
...
JpaEntityManager jem = em.unwrap(JpaEntityManager.class);
UnitOfWorkImpl ouw = jem.unwrap(UnitOfWorkImpl.class);
...
long count = countCachedEntitiesL1(clazz);
and the corresponding method:
public long countCachedEntitiesL1(Class clazz) {
long count = 0;
for (Map.Entry<Object, Object> entity : ouw.getCloneMapping().entrySet()) {
if (entity.getKey().getClass().equals(clazz)) {
count++;
}
}
return count;
}
public long countCachedEntitiesL1(Class clazz) {
return ouw.getCloneMapping().keySet().stream()
.filter(entity -> entity.getClass().equals(clazz))
.count();
}
L2 (shared cache)
import org.eclipse.persistence.jpa.JpaEntityManager;
import org.eclipse.persistence.sessions.server.ServerSession;
import org.eclipse.persistence.internal.sessions.IdentityMapAccessor;
...
JpaEntityManager jem = em.unwrap(JpaEntityManager.class);
ServerSession ss = jem.unwrap(ServerSession.class);
IdentityMapAccessor ima = (IdentityMapAccessor) ss.getIdentityMapAccessor();
...
int count = countCachedEntitiesL2(clazz);
and the corresponding method:
public int countCachedEntitiesL2(Class clazz) {
return ima.getIdentityMap(clazz).getSize();
}