Hibernate根据表名获得实体类名及ID名
2014-07-17 00:00
393 查看
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); }
相关文章推荐
- Hibernate根据表名获得实体类名及ID名
- WCF走你~一个简单的例子,根据用户ID,从用户模块(用户服务器)获得实体
- hibernate实体类操作(一)添加与根据Id查询
- Hibernate高级实体关联映射之多值的实体关联(一对多关联,利用联结表,idbag例)
- Hibernate根据实体生成数据库表
- Hibernate中根据实体查询报错Ljava.lang.Object; cannot be。。。
- 根据进程ID枚举所有窗口获得窗口句柄
- Hibernate—根据实体类和主键值得到对应的实体
- 根据流程定义名称获得流程定义ID出错
- Hibernate--根据实体类获得表名、主键名、字段名(与Spring集成)(二)
- EF中更新操作 ID自增但不是主键 ;根据ViewModel更新实体的部分属性
- 根据资源名来获得资源id
- SpringBoot中根据Hibernate的注解来关联实体之间的关系
- VC 根据线程句柄获得线程ID 根据线程句柄获得进程ID
- hibernate根据实体类获得主键属性名
- MySql如何根据输入的id获得树形结构的子节点列表:使用自连+SUBSTRING_INDEX函数
- 如何根据进程名称获得所有进程ID
- VC根据进程名获得进程ID 和 VC获得系统特殊文件夹 SHGetSpecialFolderPath 和 VC写入注册表,使程序开机启动
- 根据id查到下一级别的实体(以区域表为例子)
- 5、在MyJDBCUtils中,增加根据User实体的id,去查找User的方法