java 从 EntityManager 获取所有映射实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2007073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting all mapped Entities from EnitityManager
提问by raoulsson
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;
I want to do this for all tables. I could use select * from tabin Oracle or show tablesin MySQL to get the complete list and then move on like that.
我想对所有表都这样做。我可以select * from tab在 Oracle 或show tablesMySQL 中使用以获取完整列表,然后继续进行。
But since I already have the javax.persistence.EntityManagerObject 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).
但是因为我javax.persistence.EntityManager手头已经有了对象,我想知道是否有另一种方法来获取所有映射的实体,管理器知道(我在后台使用 Hibernate)。
回答by BryanD
There are two ways that I can see getting all of the mapped entities and their corresponding SQL tables (there may be others).
有两种方法可以让我看到获取所有映射实体及其对应的 SQL 表(可能还有其他的)。
The most straightfoward is if you can use your Hibernate Configuration object:
最直接的方法是您是否可以使用 Hibernate Configuration 对象:
for(Iterator it = config.getClassMappings(); it.hasNext();){
PersistentClass pc = (PersistentClass) it.next();
System.out.println(pc.getEntityName() + "\t" + pc.getTable().getName());
}
Alternatively, you can do a little more casting and get this same information out of the SessionFactory too:
或者,您可以进行更多的转换并从 SessionFactory 中获取相同的信息:
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);
}
回答by MarcG
As of 2016 (Hibernate 5.2), both getAllClassMetadataand Configurationare deprecated.
如2016(休眠5.2)的,两者getAllClassMetadata和Configuration被弃用。
I guess this could be used instead:
我想这可以用来代替:
Set<EntityType<?>> entities = sessionFactory.getMetamodel().getEntities();
In special, to get the classes:
特别是,要获得课程:
List<?> classes = entities.stream()
.map(EntityType::getJavaType)
.filter(Objects::nonNull)
.collect(Collectors.toList());
回答by Bozho
Check what's available in this Map:
检查此中可用的内容Map:
((Session) entityManager.getDelegate()).getSessionFactory().getAllClassMetadata() ;
回答by Sairam Krish
If you have javax.persistence.EntityManager in hand., following method can help you get list all table names:
如果你手头有 javax.persistence.EntityManager,下面的方法可以帮助你列出所有的表名:
private List<String> getAllTables() {
List<String> tableNames = new ArrayList<>();
Session session = entityManager.unwrap(Session.class);
SessionFactory sessionFactory = session.getSessionFactory();
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();
tableNames.add(tableName);
}
return tableNames;
}

