Is there a way to get all managed entities from an EntityManager

≯℡__Kan透↙ 提交于 2019-12-04 23:12:50

I think this might help:

for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) {
    final String className = entity.getName();
    log.debug("Trying select * from: " + className);
    Query q = entityManager.createQuery("from " + className + " c");
    q.getResultList().iterator();
    log.debug("ok: " + className);
}

Basically EntityManager::MetaModel contains the MetaData information regarding the Entities managed.

What JPA provider are you using?

There is nothing in the JPA API for this.

If using EclipseLink, you can use,

em.unwrap(UnitOfWorkImpl.class).getCloneMapping().keySet()

If you need to remove all entities inserted during a test, you can execute the test inside a transaction and then rollback that transaction. See 9.3.5.4 Transaction management as an example of this approach.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!