List of usage examples for org.hibernate.mapping Table getName
public String getName()
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
protected Table getTable(String tableName) { Iterator tableMappings = config.getTableMappings(); while (tableMappings.hasNext()) { Table table = (Table) tableMappings.next(); if (table.getName().equals(tableName)) { return table; }//from w ww. ja v a2 s . co m } return null; }
From source file:com.zutubi.pulse.master.transfer.TransferAPI.java
License:Apache License
/** * Run some diagnostics on the connected database, checking that all of the tables and columns in our mapping * are present and accounted for.//ww w . ja v a2 s . c o m * * @param configuration the hibernate schema mappings. * @param dataSource the datasource for the database being verified. */ private boolean isSchemaMappingValid(Configuration configuration, DataSource dataSource) throws TransferException { Connection con = null; try { try { con = dataSource.getConnection(); } catch (SQLException e) { throw new TransferException( "Failed to connect to the configured dataSource. Cause: '" + e.getMessage() + "'"); } // we could use the configuration.validateSchema() except that the output is not as friendly or comprehensive // as we would like - reporting only first error that it encounters. The downside to the custom implementation // is that it is not as comprehensive in its tests. // output diagnostics. StringBuilder builder = new StringBuilder(); builder.append("The following expected schema entities are missing from the source database:\n"); boolean problemDetected = false; boolean noTablesDetected = true; Iterator tables = configuration.getTableMappings(); while (tables.hasNext()) { Table table = (Table) tables.next(); String tableName = table.getName(); if (!JDBCUtils.tableExists(con, tableName)) { // table is missing. builder.append(" Table ").append(tableName).append(" is missing\n"); problemDetected = true; continue; } noTablesDetected = false; List<Column> missingColumns = new LinkedList<Column>(); List<Column> columns = MappingUtils.getColumns(table); for (Column column : columns) { String columnName = column.getName(); if (!JDBCUtils.columnExists(con, tableName, columnName)) { missingColumns.add(column); problemDetected = true; } } if (missingColumns.size() > 0) { builder.append(" Table ").append(tableName).append(" is missing column(s): "); String sep = ""; for (Column column : missingColumns) { builder.append(sep).append(column.getName()); sep = ", "; } builder.append("\n"); } } if (problemDetected) { if (noTablesDetected) { System.err.println("The pulse schema could not be located. " + "Please check that your database configuration details are correct."); } else { System.err.println(builder.toString()); } } return !problemDetected; } finally { JDBCUtils.close(con); } }
From source file:corner.migration.services.impl.adapter.AbstractConnectionAdapter.java
License:Apache License
@Override public TableMetadata fetchTableInfo(DatabaseMetadata meta, Table table, String defaultCatalog, String defaultSchema) {/*from w ww .j a va 2s . c o m*/ TableMetadata tableInfo = meta.getTableMetadata(table.getName(), (table.getSchema() == null) ? defaultSchema : table.getSchema(), (table.getCatalog() == null) ? defaultCatalog : table.getCatalog(), table.isQuoted()); return tableInfo; }
From source file:corner.migration.services.impl.MigrationServiceImpl.java
License:Apache License
@Override public SchemaInfo initSchemaInfo() { this.executeMigrateFragment(new MigrateFragment() { @Override//from w w w. j ava 2s . com public boolean filteTable(Table table) { return table.getName().equalsIgnoreCase(SCHEMA_INFO_TABLE_NAME); } @Override public List<String> generateMigrationFragments(Table table, TableMetadata tableInfo) { List<String> script = new ArrayList<String>(); // ?schema_info?? if (tableInfo == null) { logger.info("?[" + SCHEMA_INFO_TABLE_NAME + "],?!"); String[] sqls = cfg.generateSchemaCreationScript(dialect); executeSchemaScript(sqls); } return script; } }); // ?? HibernateTemplate hibernateTemplate = new HibernateTemplate(this.sessionFactory); SchemaInfo info = (SchemaInfo) hibernateTemplate.execute(new HibernateCallback() { /** * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session) */ @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = session.createCriteria(SchemaInfo.class); return criteria.uniqueResult(); } }); if (info == null) {// SchemaInfo // ?,version?0 info = new SchemaInfo(); info.setDbversion(DB_NOTHING); info.setIndexversion(INDEX_NOTHING); hibernateTemplate.saveOrUpdate(info); } return info; }
From source file:corner.services.migration.impl.fragment.AbstractMigrateFragment.java
License:Apache License
@Override public boolean filteTable(Table table) { return table.getName().equalsIgnoreCase(getTableName()); }
From source file:corner.services.migration.impl.fragment.ChangeColumnFragment.java
License:Apache License
String sqlAlterStrings(Table table, String columnName, Dialect dialect, Mapping p, TableMetadata tableInfo, String defaultCatalog, String defaultSchema) throws HibernateException { Iterator iter = table.getColumnIterator(); String alterSQL = null;//from w w w. j a v a 2 s. c o m while (iter.hasNext()) { Column column = (Column) iter.next(); ColumnMetadata columnInfo = tableInfo.getColumnMetadata(column.getName()); if (columnInfo != null && columnInfo.getName().toLowerCase().equals(columnName.toLowerCase())) {//,??? alterSQL = this.getConnectionAdapter().changeColumnSQL(table.getName(), getSQLType(column)); } } return alterSQL; }
From source file:corner.services.migration.impl.MigrationServiceImpl.java
License:Apache License
@Override public SchemaInfo initSchemaInfo() { this.executeMigrateFragment(new MigrateFragment() { @Override//from w ww . ja v a 2 s . c om public boolean filteTable(Table table) { return table.getName().equalsIgnoreCase(SCHEMA_INFO_TABLE_NAME); } @Override public List<String> generateMigrationFragments(Table table, TableMetadata tableInfo) { List<String> script = new ArrayList<String>(); // ?schema_info?? if (tableInfo == null) { logger.info("?[" + SCHEMA_INFO_TABLE_NAME + "],?!"); String[] sqls = cfg.generateSchemaCreationScript(dialect); executeSchemaScript(sqls); } return script; } }); // ?? HibernateTemplate hibernateTemplate = new HibernateTemplate(this.sessionFactory); SchemaInfo info = (SchemaInfo) hibernateTemplate.execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = session.createCriteria(SchemaInfo.class); return criteria.uniqueResult(); } }); if (info == null) {// SchemaInfo // ?,version?0 info = new SchemaInfo(); info.setDbversion(DB_NOTHING); info.setIndexversion(INDEX_NOTHING); hibernateTemplate.saveOrUpdate(info); } return info; }
From source file:de.xwic.sandbox.server.installer.modules.UpdateColumnSizes.java
License:Apache License
public void run(InstallationManager manager) throws Exception { IDatabaseHandler dbHandler = manager.getDatabaseHandler(); Configuration cfg = HibernateUtil.getConfiguration(); for (Iterator<?> it = cfg.getTableMappings(); it.hasNext();) { Table table = (Table) it.next(); List<DbColumn> list = dbHandler.getTableColumns(table.getName()); // put it into a map for easier access Map<String, DbColumn> map = new HashMap<String, DbColumn>(); for (Iterator<DbColumn> itL = list.iterator(); itL.hasNext();) { DbColumn col = itL.next();//from w ww. j a v a 2s. c om map.put(col.getName(), col); } for (Iterator<?> itC = table.getColumnIterator(); itC.hasNext();) { Column column = (Column) itC.next(); DbColumn dbCol = map.get(column.getName()); if (dbCol != null && "varchar".equalsIgnoreCase(dbCol.getDataType()) && dbCol.getLength() != column.getLength()) { if (dbCol.getLength() > column.getLength()) { log.warn("The column '" + column.getName() + "' in table '" + table.getName() + "' is larger (" + dbCol.getLength() + ") then specified (" + column.getLength() + ") in the mapping. The column size is not modified. "); } else { log.info("Changing column size for '" + table.getName() + "." + column.getName() + "' from " + dbCol.getLength() + " to " + column.getLength()); dbHandler.alterColumn(table.getName(), dbCol.getName(), "VARCHAR(" + column.getLength() + ")"); } } } } }
From source file:edu.wustl.common.util.dbManager.HibernateMetaData.java
License:BSD License
public static String getTableName(Class classObj) { Table tbl = cfg.getClassMapping(classObj.getName()).getTable(); if (tbl != null) return tbl.getName(); return ""; }
From source file:edu.wustl.common.util.dbManager.HibernateMetaData.java
License:BSD License
public static String getRootTableName(Class classObj) { Table tbl = cfg.getClassMapping(classObj.getName()).getRootTable(); if (tbl != null) return tbl.getName(); return ""; }