完美解决hibernatetool oracle注释问题


我的模板工具一直不能完美的将oracle的注释生成到我的代码中,

失眠时解决了这个问题,记录下供大家参考

不知道是不是我的hibernatetool比较旧有这个问题

配置hibernatetool.metadatadialect=org.hibernate.cfg.reveng.dialect.OracleMetaDataDialect决定代码使用OracleMetaDataDialect获取数据库表结构

否则使用jdbc方式,这样是不能得到commont的

修改OracleMetaDataDialect类的2个方法

所以大家可以看到人家压根就没有实现oracle获取注释的功能呢

	public Iterator getTables(final String catalog, final String schema, String table) {
		 final StringBuffer query = new StringBuffer();
		try {		
			log.debug("getTables(" + catalog + "." + schema + "." + table + ")");
			// Collect both Tables and Views from the 'ALL' data dicitonary tables.
			// Note: This will potentally collect more tables that the jdbc meta data 
	        Statement stmt = this.getConnection().createStatement();
	        query.append("select  t.*,tc.comments as REMARKS from ( ");
	        query.append("select  table_name, owner, 'TABLE' from all_tables ");
	        if (schema != null || table != null)
	        	query.append("where ");
			if (schema != null) {
				query.append("owner='" + schema + "' ");
			}
			if (table != null) {
				if (schema != null)
					query.append("and ");
				query.append("table_name = '" + table + "' ");
			}
			query.append("union all ");
			query.append("select view_name, owner, 'VIEW' from all_views ");
	        if (schema != null || table != null)
	        	query.append("where ");
			if (schema != null) {
				query.append("owner='" + schema + "' ");
			}
			if (table != null) {
				if (schema != null)
					query.append("and ");
				query.append("view_name = '" + table + "' ");
			}
			query.append(" )t ");
			query.append(" ,USER_TAB_COMMENTS tc where t.table_name=tc.table_name ");
			if (log.isDebugEnabled())
				log.debug("getTables Query:" + query.toString());
			
	        ResultSet tableRs = stmt.executeQuery(query.toString());
			
			return new ResultSetIterator(stmt, tableRs, getSQLExceptionConverter()) {
				
				Map element = new HashMap();
				protected Object convertRow(ResultSet tableRs) throws SQLException {
					element.clear();
					element.put("TABLE_NAME", tableRs.getString(1));
					element.put("TABLE_SCHEM", tableRs.getString(2));
					element.put("TABLE_CAT", null);
					element.put("TABLE_TYPE", tableRs.getString(3));
					element.put("REMARKS", tableRs.getString("REMARKS"));
					return element;					
				}
				protected Throwable handleSQLException(SQLException e) {
					// schemaRs and catalogRs are only used for error reporting if
					// we get an exception
					String databaseStructure = getDatabaseStructure( catalog, schema );
					throw getSQLExceptionConverter().convert( e,
							"Could not get list of tables from database. Probably a JDBC driver problem. "+query.toString()
									+ databaseStructure, null );					
				}
			};
		} catch (SQLException e) {
			// schemaRs and catalogRs are only used for error reporting if we get an exception
			String databaseStructure = getDatabaseStructure(catalog,schema);
			throw getSQLExceptionConverter().convert(e, "Could not get list of tables from database. Probably a JDBC driver problem. "+query.toString() + databaseStructure, null);		         
		} 		
	}

	public Iterator getColumns(final String catalog, final String schema, final String table, String column) {
		final StringBuffer query = new StringBuffer();
		try {			  
			log.debug("getColumns(" + catalog + "." + schema + "." + table + "." + column + ")");
			// Collect Columns from the 'ALL' data dicitonary table.
			// A decode is used to map the type name to the JDBC Type ID
	        Statement stmt = this.getConnection().createStatement();
	        

	        query.append("select t.column_name as COLUMN_NAME, t.owner as TABLE_SCHEM, decode(t.nullable,'N',0,1) as NULLABLE, ");
	        query.append("decode(t.data_type, 'FLOAT',decode(t.data_precision,null, t.data_length, t.data_precision), 'NUMBER', decode(t.data_precision,null, t.data_length, t.data_precision), t.data_length) as COLUMN_SIZE, ");
	        query.append("decode(t.data_type,'CHAR',1, 'DATE',91, 'FLOAT',6, 'LONG',-1, 'NUMBER',2, 'VARCHAR2',12, 'BFILE',-13, ");
	        query.append("'BLOB',2004, 'CLOB',2005, 'MLSLABEL',1111, 'NCHAR',1, 'NCLOB',2005, 'NVARCHAR2',12, ");
	        query.append("'RAW',-3, 'ROWID',1111, 'UROWID',1111, 'LONG RAW', -4, 'TIMESTAMP', 93, 'XMLTYPE',2005, 1111) as DATA_TYPE, ");
	        query.append("t.table_name as TABLE_NAME, t.data_type as TYPE_NAME, decode(t.data_scale, null, 0 ,data_scale) as DECIMAL_DIGITS ");
	        query.append(",tc.COMMENTS as REMARKS ");
	        query.append("from all_tab_columns t");
	        query.append(", USER_COL_COMMENTS tc");	   
	        
	        query.append(" where 1=1 and t.TABLE_NAME=tc.TABLE_NAME and t.COLUMN_NAME=tc.COLUMN_NAME ");
	        if (schema != null || table != null || column != null)
	        	
			if (schema != null) {
				query.append(" and t.owner='" + schema + "' ");
			}
			if (table != null) {
				if (schema != null)
					query.append("and ");
				query.append(" t.table_name = '" + table + "' ");
			}
			if (column != null) {
				if (schema != null || table != null)
					query.append("and ");
				query.append(" t.column_name = '" + column + "' ");				
			}
			query.append("order by t.column_id ");
			if (log.isDebugEnabled())
				log.debug("getIndexInfo Query:" + query.toString());
			
	        ResultSet columnRs = stmt.executeQuery(query.toString());
			
			return new ResultSetIterator(stmt, columnRs, getSQLExceptionConverter()) {
				
				Map element = new HashMap();
				protected Object convertRow(ResultSet rs) throws SQLException {
					element.clear();
					element.put("COLUMN_NAME", rs.getString(1));
					element.put("TABLE_SCHEM", rs.getString(2));
					element.put("NULLABLE", new Integer(rs.getInt(3)));
					element.put("COLUMN_SIZE", new Integer(rs.getInt(4)));
					element.put("DATA_TYPE", new Integer(rs.getInt(5)));
					element.put("TABLE_NAME", rs.getString(6));
					element.put("TYPE_NAME", rs.getString(7));
					element.put("DECIMAL_DIGITS", new Integer(rs.getInt(8)));					
					element.put("TABLE_CAT", null);
					element.put("REMARKS", rs.getString("REMARKS"));
					
					return element;
				}
				protected Throwable handleSQLException(SQLException e) {
					throw getSQLExceptionConverter().convert(e, "Error while reading column meta data for " + Table.qualify(catalog, schema, table)+query.toString(), null);
				}
			};
		} catch (SQLException e) {
			throw getSQLExceptionConverter().convert(e, "Error while reading column meta data for " + Table.qualify(catalog, schema, table)+query.toString(), null);
		}	
	}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值