Hi, how can I get the table name of an entity in Hibernate 6.2+? I used this in Hibernate 5.6:
public static String getEntityTableName(final EntityManager entityManager,
final Class<?> clazz) {
final Session session = entityManager.unwrap(Session.class);
final SessionFactory sessionFactory = session.getSessionFactory();
final ClassMetadata hibernateMetadata = sessionFactory
.getClassMetadata(clazz);
final AbstractEntityPersister persister = (AbstractEntityPersister) hibernateMetadata;
if (persister == null) {
return null;
}
return persister.getTableName();
}
beikov
November 27, 2023, 5:49pm
2
You can use AbstractEntityPersister#getTableNames()
, but this is all pretty much internal stuff, so you will have to dig into the code to ensure you’re getting what you expect.
From which class can I get the AbstractEntityPersister
? MetamodelImplementor
is deprecated and MappingMetamodel
is incubating.
beikov
November 27, 2023, 6:08pm
4
AbstractEntityPersister
is internal, so using an incubating API is the least of your problems
You are right , but is there no external way to get the table name?
beikov
November 28, 2023, 8:00am
6
There is no supported way to do this.
Can you tell me more details about the supported way?
beikov
November 28, 2023, 3:31pm
8
The “no” must have slipped when I typed this. There is no supported way.
1 Like