有没有办法从EntityManager获取所有托管实体 [英] Is there a way to get all managed entities from an EntityManager

查看:132
本文介绍了有没有办法从EntityManager获取所有托管实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个基本的测试数据工具,并希望跟踪EntityManager处理的所有数据.不仅仅是每个实体都有一堆列表,还有一种方法可以一举抓住由EntityManager管理的所有内容吗?

I'm setting up a basic test data util and want to keep track of all the data that the EntityManager handles. Rather than just having a bunch of lists for each entity is there a way to grab everything being managed by the EntityManager in one fell swoop?

所以代替这个:

EntityManager em;
List<Entity1> a;
List<Entity2> b;
...
List<Entityn> n;

cleanup() {
    for(Entity1 e : a) em.remove(e);
    for(Entity2 f : b) em.remove(f);
    ...
    for(Entityn z : n) em.remove(z);
}

我想要这样的东西;

EntityManager em;

cleanup() {
    List<Object> allEntities = em.getAllManagedEntities(); //<-this doesnt exist
    for(Object o : allEntities) em.remove(o);
}

不确定这是否可行,但我只是想像一下经理知道他在管理什么?或者,如果您有轻松管理一堆实体的想法.

Not sure if this is possible, but I just would image that the manager knows what it is managing? Or, if you have any ideas of managing a bunch of entities easily.

推荐答案

我认为这可能会有所帮助:

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);
}

基本上 EntityManager :: MetaModel 包含有关所管理实体的元数据信息.

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

这篇关于有没有办法从EntityManager获取所有托管实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆