Hibernate通过实体对象对应数据库表信息,包括:数据库表名称、主键列名、非主键列名等。

获取对象映射缓存管理类:

AbstractEntityPersister aep = (AbstractEntityPersister)getSessionFactory().getClassMetadata(model.getClass());

获取对象名称:

String entityName = aep.getEntityName();

获取映射表名:

String tableName = aep.getTableName();

获取主键列映射属性:

String pkAttr = aep.getIdentifierPropertyName();

获取主键列名:

String[] pkCols = aep.getIdentifierColumnNames();

获取非主键属性:

String[] properties = aep.getPropertyNames();

获取非主键字段名称:

for(int idx=0; idx<properties.length; idx++) {
// 通过索引获取列名
String[] colname = aep.getPropertyColumnNames(idx);
// 通过属性名称获取列名
String propertyName = properties[idx];
colname = aep.getPropertyColumnNames(propertyName);
}

参考网址:

https://www.cnblogs.com/lmjk/articles/4127664.html

https://www.cnblogs.com/itmyhome/p/4131396.html

05-11 17:30