Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...