从EntityManager获取所有映射的实体 [英] Getting all mapped Entities from EntityManager
问题描述
我有一段维护代码,应在特定时间点向特定用户授予选择特权:
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 table
来获取完整列表,然后像
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
对象,我想知道是否有经理知道了另一种获取所有映射实体的方法(我在后台使用Hibernate)。
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);
}
这篇关于从EntityManager获取所有映射的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!