Getting all mapped Entities from EnitityManager

不羁岁月 提交于 2019-12-04 03:31:15

问题


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 tab in Oracle or show tables in MySQL to get the complete list and then move on like that.

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).


回答1:


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

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

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



回答2:


As of 2016 (Hibernate 5.2), both getAllClassMetadata and Configuration are deprecated.

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



回答3:


Check what's available in this Map:

((Session) entityManager.getDelegate()).getSessionFactory().getAllClassMetadata() ;



回答4:


If you have javax.persistence.EntityManager in hand., following method can help you get list all table names:

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


来源:https://stackoverflow.com/questions/2007073/getting-all-mapped-entities-from-enititymanager

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