Пример кода для получения имени таблицы для объекта в JPA во время выполнения?
Я хочу перечислить все имена таблиц базы данных для моей модели сущности JPA, но не могу получить правильный класс сущностей!
EntityManagerFactory factory;
Set<EntityType<?>> entities = factory.getMetamodel().getEntities();
for (EntityType entity: entities) {
String tableName = entity.getClass().getAnnotation(Table.class).name();
logger.debug("Entity name = {}", entity.getName(); //This works
logger.debug("Entity class = {}", entity.getClass().getName()); //This returns the runtime class of the object, and not the entity class!!
logger.debug("Entity table = {}", entity.getClass().getAnnotation(Table.class).name()); //Nothing, because it does not find the correct class
}
Выход:
Entity name = PersonEntity
Entity class = org.hibernate.jpa.internal.metamodel.EntityTypeImpl
Entity table = ........ nothing, because this works on the EntityTypeImpl and not on the PersonEnity
Как получить имя таблицы для класса сущностей?