List of usage examples for org.hibernate.mapping Column getName
public String getName()
From source file:com.wm.framework.sh.dao.impl.BaseDaoImpl.java
License:Open Source License
protected String getColumnName(Class<?> clazz, String propertyName) { PersistentClass persistentClass = getPersistentClass(clazz); Property property = persistentClass.getProperty(propertyName); Iterator<?> it = property.getColumnIterator(); if (it.hasNext()) { Column column = (Column) it.next(); return column.getName(); }//from ww w . j av a2 s. c o m return null; }
From source file:com.xpn.xwiki.store.migration.hibernate.AbstractDropNotNullDataMigration.java
License:Open Source License
@Override public String getLiquibaseChangeLog() throws DataMigrationException { XWikiHibernateBaseStore store = getStore(); Dialect dialect = store.getDialect(); Configuration configuration = store.getConfiguration(); Mapping mapping = configuration.buildMapping(); PersistentClass pClass = configuration.getClassMapping(this.table.getName()); Column column = ((Column) pClass.getProperty(this.property).getColumnIterator().next()); String columnType = column.getSqlType(dialect, mapping); StringBuilder builder = new StringBuilder(); builder.append("<changeSet author=\"xwiki\" id=\"R").append(this.getVersion().getVersion()).append("\">\n"); builder.append(" <dropNotNullConstraint\n"); builder.append(" columnDataType=\"").append(columnType).append('"').append('\n'); builder.append(" columnName=\"").append(column.getName()).append('"').append('\n'); builder.append(" tableName=\"").append(pClass.getTable().getName()).append("\"/>\n"); builder.append("</changeSet>"); return builder.toString(); }
From source file:com.xpn.xwiki.store.migration.hibernate.R40000XWIKI6990DataMigration.java
License:Open Source License
/** * Append a add primary key constraint command for the given table. * * @param sb append the result into this string builder * @param table the table name//from w w w . j a va 2 s . c om */ private void appendAddPrimaryKey(StringBuilder sb, Table table) { PrimaryKey pk = table.getPrimaryKey(); String pkName = pk.getName(); sb.append(" <addPrimaryKey tableName=\"").append(table.getName()).append("\" columnNames=\""); @SuppressWarnings("unchecked") Iterator<Column> columns = pk.getColumnIterator(); while (columns.hasNext()) { Column column = columns.next(); sb.append(column.getName()); if (columns.hasNext()) { sb.append(","); } } if (pkName != null) { sb.append("\" constraintName=\"").append(pkName); } sb.append("\"/>\n"); }
From source file:com.xpn.xwiki.store.migration.hibernate.R40000XWIKI6990DataMigration.java
License:Open Source License
/** * Append a add index command for the given index. * * @param sb append the result into this string builder * @param index the index//from w w w. j a v a 2 s.c om */ private void appendAddIndex(StringBuilder sb, Index index) { sb.append(" <createIndex tableName=\"").append(index.getTable().getName()).append("\" indexName=\"") .append(index.getName()).append("\">\n"); @SuppressWarnings("unchecked") Iterator<Column> columns = index.getColumnIterator(); while (columns.hasNext()) { Column column = columns.next(); sb.append(" <column name=\"").append(column.getName()).append("\"/>\n"); } sb.append("</createIndex>\n"); }
From source file:com.xpn.xwiki.store.migration.hibernate.R40000XWIKI6990DataMigration.java
License:Open Source License
/** * Append change log to add foreign keys with CASCADEd updates. * * @param sb the string builder to append to the add tasks * @param table the table to process// ww w . j a v a 2s. c o m */ @SuppressWarnings("unchecked") private void appendAddForeignKeyChangeLog(StringBuilder sb, Table table) { Iterator<ForeignKey> fki = table.getForeignKeyIterator(); // Preamble String tableName = table.getName(); sb.append(" <changeSet id=\"R").append(this.getVersion().getVersion()).append('-') .append(Util.getHash(String.format("addForeignKeyConstraint-%s", tableName))) .append("\" author=\"xwiki\" runOnChange=\"true\" runAlways=\"true\">\n") .append(" <comment>Add foreign keys on table [").append(tableName) .append("] to use ON UPDATE CASCADE</comment>\n"); // Concrete Property types should each have a foreign key referencing the BaseProperty // Other classes don't have any foreign keys at all, in which case the fast exit path above was used while (fki.hasNext()) { ForeignKey fk = fki.next(); if (fk.isReferenceToPrimaryKey()) { // Recreate the constraint sb.append(" <addForeignKeyConstraint constraintName=\"").append(fk.getName()) .append("\" baseTableName=\"").append(tableName).append("\" baseColumnNames=\""); // Reuse the data from the old foreign key // Columns in the current table Iterator<Column> columns = fk.getColumnIterator(); while (columns.hasNext()) { Column column = columns.next(); sb.append(column.getName()); if (columns.hasNext()) { sb.append(","); } } sb.append("\" referencedTableName=\"").append(fk.getReferencedTable().getName()) .append("\" referencedColumnNames=\""); // Columns in the referenced table columns = fk.getReferencedTable().getPrimaryKey().getColumnIterator(); while (columns.hasNext()) { Column column = columns.next(); sb.append(column.getName()); if (columns.hasNext()) { sb.append(","); } } // The important part: cascaded updates if (this.isOracle) { // Oracle doesn't support cascaded updates, but allow the constraint to be checked // at the commit level (normal checking is done at the statement level). sb.append("\" initiallyDeferred=\"true\"/>\n"); } else { sb.append("\" onUpdate=\"CASCADE\"/>\n"); } } } // All done! sb.append(" </changeSet>\n"); this.logCount++; }
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
/** * Patch the existing configuration. These patches are 'additive' only. Any columns * or tables found in the patch that do not already exist will be added. Anything missing * will be ignored./* www.j a va 2 s . c om*/ * * @param mapping the classpath reference to the patch hbm.xml file * * @throws IOException if there is a problem loading the patch file. */ public void patch(String mapping) throws IOException { // Ok, so we do this as follows: // a) load the patch mapping // b) apply it to the existing in memory mapping // c) sync the in memory mapping with the database. // Note: this falls under the same restrictions as normal hibernate syncs, so // any columns that change details will not need to be handled separately. Only // column additions and table additions are included. MutableConfiguration config = new MutableConfiguration(); config.addClassPathMappings(Arrays.asList(mapping)); config.buildMappings(); Iterator tables = config.getTableMappings(); while (tables.hasNext()) { Table table = (Table) tables.next(); // do we have this table?. Table existingTable = getTable(table.getName()); if (existingTable != null) { Iterator columns = table.getColumnIterator(); while (columns.hasNext()) { Column column = (Column) columns.next(); Column existingColumn = getColumn(existingTable, column.getName()); if (existingColumn == null) { existingTable.addColumn(column); } else { // avoid this case for now - we need to refresh the column with the new definition, // which means updating the in memory column configuration, and then running a refresh. } } } else { this.config.addTable(table); } } sync(); }
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
private Column getColumn(Table table, String columnName) { Iterator columns = table.getColumnIterator(); while (columns.hasNext()) { Column column = (Column) columns.next(); if (column.getName().equals(columnName)) { return column; }/*from w w w . jav a 2 s .c o m*/ } return null; }
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
private void renameColumn(Connection connection, Table table, Column fromColumn, String toColumnName) throws SQLException { // a) identify foreign key references. List<ForeignKey> droppedConstraints = dropColumnConstraints(connection, table, fromColumn); // update table model. String fromColumnName = fromColumn.getName(); fromColumn.setName(toColumnName);/*from w w w .ja v a 2s .c om*/ // add the new column to the table - synchronise the database with the updated schema. updateTableSchema(table, connection); // copy column data. sqlCopyColumn(connection, table.getName(), toColumnName, fromColumnName); // recreate the foreign key constraint if it exists. recreatedDroppedConstraints(droppedConstraints, connection); // d) drop the original column. sqlDropColumn(connection, table, fromColumnName); }
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
private void refreshColumn(Connection connection, Table table, Column column) throws SQLException { // create a temporary column. Make a temporary change to the in-memory schema to allow this. String columnName = column.getName(); String tmpColumnName = "temporary_" + columnName; try {//from w ww . j a v a 2s . c o m column.setName(tmpColumnName); updateTableSchema(table, connection); } finally { column.setName(columnName); } // copy data to the temporary column. sqlCopyColumn(connection, table.getName(), tmpColumnName, columnName); // drop the existing column constraints. List<ForeignKey> droppedConstraints = dropColumnConstraints(connection, table, column); // drop original column. sqlDropColumn(connection, table, columnName); // recreate column - with the refreshed schema. updateTableSchema(table, connection); // copy the data back to the refreshed column. sqlCopyColumn(connection, table.getName(), columnName, tmpColumnName); // re-enable the foreign key constraints. recreatedDroppedConstraints(droppedConstraints, connection); // drop the temporary column. sqlDropColumn(connection, table, tmpColumnName); }
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
private void metaDropColumnConstraints(Connection connection, Table table, Column column) throws SQLException { JDBCUtils.DbType dbType = JDBCUtils.getDBType(connection); if (dbType == JDBCUtils.DbType.HSQL) { hsqlDropColumnConstraints(connection, table, column); } else {/* www.ja va2s . c o m*/ String[] tableNames = JDBCUtils.getSchemaTableNames(connection); String metaTableName = null; for (String tableName : tableNames) { if (tableName.equalsIgnoreCase(table.getName())) { metaTableName = tableName; } } if (metaTableName == null) { throw new RuntimeException("Could not find table of name '" + table.getName() + "' in schema"); } DatabaseMetaData metaData = connection.getMetaData(); ResultSet rs = metaData.getIndexInfo(null, null, metaTableName, false, false); try { while (rs.next()) { if (column.getName().equalsIgnoreCase(rs.getString("COLUMN_NAME"))) { String indexName = rs.getString("INDEX_NAME"); if (indexName.startsWith("FK") && dbType == JDBCUtils.DbType.MYSQL) { tryDropForeignKey(connection, metaTableName, indexName); } tryDropIndex(connection, metaTableName, indexName); } } } finally { JDBCUtils.close(rs); } } }