Get table name in Hibernate 6.2+

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

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.

AbstractEntityPersister is internal, so using an incubating API is the least of your problems :wink:

You are right :innocent:, but is there no external way to get the table name?

There is no supported way to do this.

Can you tell me more details about the supported way? :flushed:

The “no” must have slipped when I typed this. There is no supported way.

1 Like