private Map<String, String> mappings;
private Map<String, String> idMappings;
private Map<String, String> idColumns;
public void initMappings() {
if (mappings == null) {
mappings = new HashMap<String, String>();
idMappings = new HashMap<String, String>();
idColumns = new HashMap<String, String>();
SessionFactory factory = this.getSessionFactory();
Map metaMap = factory.getAllClassMetadata();
for (String key : (Set<String>) metaMap.keySet()) {
AbstractEntityPersister classMetadata = (AbstractEntityPersister) metaMap
.get(key);
String tableName = classMetadata.getTableName().toLowerCase();
int index = tableName.indexOf(".");
if (index >= 0) {
tableName = tableName.substring(index + 1);
}
String className = classMetadata.getEntityMetamodel().getName();
String idName = classMetadata.getIdentifierColumnNames()[0];
mappings.put(tableName.toUpperCase(), className);
idColumns.put(tableName.toUpperCase(), idName);
idMappings.put(className,
classMetadata.getIdentifierPropertyName());
}
}
}
public String getEntityName(String tableName) throws Exception {
initMappings();
String entityName = mappings.get(tableName.toUpperCase());
if (entityName == null) {
throw new Exception("表没有映射:" + tableName);
}
return entityName;
}
public String getIdName(String entityName) {
initMappings();
return idMappings.get(entityName);
}
public String getIdColumn(String tableName) {
initMappings();
return idColumns.get(tableName);
}
转载于:https://my.oschina.net/javawdw/blog/291941