Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...