在Hibernate中从模型中获取表名

在Hibernate中从模型中获取表名

本文介绍了在Hibernate中从模型中获取表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



显然以前有一个 getTableName() ,但它已被删除。



有一个,但我不知道如何(或者如果我应该)在我的DAO实现中使用Configuration。



我的DAO实现是。



更新:事实证明,我可以在没有表名的情况下执行操作。然而,我会保持这个问题的公开性(并且在他们来的时候尝试答案),以供参考。

这是一个有点奇怪,但它的工作原理:

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


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.

解决方案

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

这篇关于在Hibernate中从模型中获取表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:06