Java – Get the table name from the model in Hibernate

hibernatejava

How do I get the table name for a model in Hibernate?

Apparently there used to be a getTableName() method in ClassMetadata, but it's been removed.

There's a getClassMapping(String entityName) method in Configuration, but I don't know how I can (or if I should) use Configuration from within my DAO implementation.

My DAO implementation is a subclass of HibernateGeneralGenericDao.

UPDATE: It turns out I can do what I'm trying to do without the table name. However, I will keep the question open (and try the answers as they come) for the sake of reference.

Best Answer

It's a bit weird but it works:

ClassMetadata hibernateMetadata = sessionFactory.getClassMetadata(pClassName);

if (hibernateMetadata == null)
{
    return;
}

if (hibernateMetadata instanceof AbstractEntityPersister)
{
     AbstractEntityPersister persister = (AbstractEntityPersister) hibernateMetadata;
     String tableName = persister.getTableName();
     String[] columnNames = persister.getKeyColumnNames();
}