从EnitityManager获取所有映射实体 [英] Getting all mapped Entities from EnitityManager

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

问题描述

我有一段维护代码,应该在某些时间点向特定用户授予选择权限:

I have a piece of maintenance code that should grant select privileges to a certain user at certain points of time:

grant select on A_DB.A_TABLE to READ_ONLY_USER;

我想对所有表执行此操作。我可以使用Oracle中的 select * from tab 或MySQL中的 show tables 获得完整的列表,然后继续

I want to do this for all tables. I could use select * from tab in Oracle or show tables in MySQL to get the complete list and then move on like that.

但是由于我已经有 javax.persistence.EntityManager Object,我想知道是否有

But since I already have the javax.persistence.EntityManager Object at hand, I wondered if there is another way to get at all the mapped Entities, the Manager knows about (I am using Hibernate under the hood).

推荐答案

另一种方式获得所有映射的实体,管理器知道是两种方法,我可以看到获得所有映射的实体及其对应的SQL表(可能有其他)。

There are two ways that I can see getting all of the mapped entities and their corresponding SQL tables (there may be others).

最直接的是如果你可以使用你的Hibernate配置对象:

The most straightfoward is if you can use your Hibernate Configuration object:

    for(Iterator it = config.getClassMappings(); it.hasNext();){
        PersistentClass pc = (PersistentClass) it.next();
        System.out.println(pc.getEntityName() + "\t" + pc.getTable().getName());
    }

或者,您可以再多做一次投放, SessionFactory:

Alternatively, you can do a little more casting and get this same information out of the SessionFactory too:

    Map<String, ClassMetadata>  map = (Map<String, ClassMetadata>) sessionFactory.getAllClassMetadata();
    for(String entityName : map.keySet()){
        SessionFactoryImpl sfImpl = (SessionFactoryImpl) sessionFactory;
        String tableName = ((AbstractEntityPersister)sfImpl.getEntityPersister(entityName)).getTableName();
        System.out.println(entityName + "\t" + tableName);
    }

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

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