Author: steve.ebersole(a)jboss.com
Date: 2009-06-01 17:50:17 -0400 (Mon, 01 Jun 2009)
New Revision: 16659
Modified:
core/trunk/core/src/main/java/org/hibernate/cfg/Configuration.java
core/trunk/core/src/main/java/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java
core/trunk/core/src/main/java/org/hibernate/tool/hbm2ddl/TableMetadata.java
Log:
HHH-3532 - schema update task should look for foreign key signature
Modified: core/trunk/core/src/main/java/org/hibernate/cfg/Configuration.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/cfg/Configuration.java 2009-06-01 21:50:00
UTC (rev 16658)
+++ core/trunk/core/src/main/java/org/hibernate/cfg/Configuration.java 2009-06-01 21:50:17
UTC (rev 16659)
@@ -1048,7 +1048,7 @@
ForeignKey fk = (ForeignKey) subIter.next();
if ( fk.isPhysicalConstraint() ) {
boolean create = tableInfo == null || (
- tableInfo.getForeignKeyMetadata( fk.getName() ) == null && (
+ tableInfo.getForeignKeyMetadata( fk ) == null && (
//Icky workaround for MySQL bug:
!( dialect instanceof MySQLDialect ) ||
tableInfo.getIndexMetadata( fk.getName() ) == null
Modified:
core/trunk/core/src/main/java/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java
===================================================================
---
core/trunk/core/src/main/java/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java 2009-06-01
21:50:00 UTC (rev 16658)
+++
core/trunk/core/src/main/java/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java 2009-06-01
21:50:17 UTC (rev 16659)
@@ -20,39 +20,73 @@
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
- *
*/
package org.hibernate.tool.hbm2ddl;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import org.hibernate.mapping.Column;
+import org.hibernate.mapping.ForeignKey;
+
/**
* JDBC foreign key metadata
+ *
* @author Christoph Sturm
*/
public class ForeignKeyMetadata {
private final String name;
- private final List columns = new ArrayList();
+ private final String refTable;
+ private final Map references = new HashMap();
ForeignKeyMetadata(ResultSet rs) throws SQLException {
name = rs.getString("FK_NAME");
+ refTable = rs.getString("PKTABLE_NAME");
}
public String getName() {
return name;
}
- void addColumn(ColumnMetadata column) {
- if (column != null) columns.add(column);
+ public String getReferencedTableName() {
+ return refTable;
}
- public ColumnMetadata[] getColumns() {
- return (ColumnMetadata[]) columns.toArray(new ColumnMetadata[0]);
+ void addReference(ResultSet rs) throws SQLException {
+ references.put( rs.getString("FKCOLUMN_NAME").toLowerCase(),
rs.getString("PKCOLUMN_NAME") );
}
+ private boolean hasReference(Column column, Column ref) {
+ String refName = (String) references.get(column.getName().toLowerCase());
+ return ref.getName().equalsIgnoreCase(refName);
+ }
+
+ public boolean matches(ForeignKey fk) {
+ if ( refTable.equalsIgnoreCase( fk.getReferencedTable().getName() ) ) {
+ if ( fk.getColumnSpan() == references.size() ) {
+ List fkRefs;
+ if ( fk.isReferenceToPrimaryKey() ) {
+ fkRefs = fk.getReferencedTable().getPrimaryKey().getColumns();
+ }
+ else {
+ fkRefs = fk.getReferencedColumns();
+ }
+ for ( int i = 0; i < fk.getColumnSpan(); i++ ) {
+ Column column = fk.getColumn( i );
+ Column ref = ( Column ) fkRefs.get( i );
+ if ( !hasReference( column, ref ) ) {
+ return false;
+ }
+ }
+ return true;
+ }
+ }
+ return false;
+ }
+
public String toString() {
return "ForeignKeyMetadata(" + name + ')';
}
Modified: core/trunk/core/src/main/java/org/hibernate/tool/hbm2ddl/TableMetadata.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/tool/hbm2ddl/TableMetadata.java 2009-06-01
21:50:00 UTC (rev 16658)
+++ core/trunk/core/src/main/java/org/hibernate/tool/hbm2ddl/TableMetadata.java 2009-06-01
21:50:17 UTC (rev 16659)
@@ -20,7 +20,6 @@
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
- *
*/
package org.hibernate.tool.hbm2ddl;
@@ -29,16 +28,20 @@
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
+import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.hibernate.mapping.ForeignKey;
+
/**
* JDBC table metadata
- * @author Christoph Sturm, Max Rydahl Andersen
+ *
+ * @author Christoph Sturm
+ * @author Max Rydahl Andersen
*/
public class TableMetadata {
-
private static final Logger log = LoggerFactory.getLogger(TableMetadata.class);
private final String catalog;
@@ -91,6 +94,17 @@
return (ForeignKeyMetadata) foreignKeys.get( keyName.toLowerCase() );
}
+ public ForeignKeyMetadata getForeignKeyMetadata(ForeignKey fk) {
+ Iterator it = foreignKeys.values().iterator();
+ while ( it.hasNext() ) {
+ ForeignKeyMetadata existingFk = ( ForeignKeyMetadata ) it.next();
+ if ( existingFk.matches( fk ) ) {
+ return existingFk;
+ }
+ }
+ return null;
+ }
+
public IndexMetadata getIndexMetadata(String indexName) {
return (IndexMetadata) indexes.get( indexName.toLowerCase() );
}
@@ -98,7 +112,9 @@
private void addForeignKey(ResultSet rs) throws SQLException {
String fk = rs.getString("FK_NAME");
- if (fk == null) return;
+ if (fk == null) {
+ return;
+ }
ForeignKeyMetadata info = getForeignKeyMetadata(fk);
if (info == null) {
@@ -106,13 +122,15 @@
foreignKeys.put( info.getName().toLowerCase(), info );
}
- info.addColumn( getColumnMetadata( rs.getString("FKCOLUMN_NAME") ) );
+ info.addReference( rs );
}
private void addIndex(ResultSet rs) throws SQLException {
String index = rs.getString("INDEX_NAME");
- if (index == null) return;
+ if (index == null) {
+ return;
+ }
IndexMetadata info = getIndexMetadata(index);
if (info == null) {
@@ -126,7 +144,9 @@
public void addColumn(ResultSet rs) throws SQLException {
String column = rs.getString("COLUMN_NAME");
- if (column==null) return;
+ if (column==null) {
+ return;
+ }
if ( getColumnMetadata(column) == null ) {
ColumnMetadata info = new ColumnMetadata(rs);
@@ -139,10 +159,14 @@
try {
rs = meta.getImportedKeys(catalog, schema, name);
- while ( rs.next() ) addForeignKey(rs);
+ while ( rs.next() ) {
+ addForeignKey(rs);
+ }
}
finally {
- if (rs != null) rs.close();
+ if (rs != null) {
+ rs.close();
+ }
}
}
@@ -153,12 +177,16 @@
rs = meta.getIndexInfo(catalog, schema, name, false, true);
while ( rs.next() ) {
- if ( rs.getShort("TYPE") == DatabaseMetaData.tableIndexStatistic )
continue;
+ if ( rs.getShort("TYPE") == DatabaseMetaData.tableIndexStatistic ) {
+ continue;
+ }
addIndex(rs);
}
}
finally {
- if (rs != null) rs.close();
+ if (rs != null) {
+ rs.close();
+ }
}
}
@@ -167,10 +195,14 @@
try {
rs = meta.getColumns(catalog, schema, name, "%");
- while ( rs.next() ) addColumn(rs);
+ while ( rs.next() ) {
+ addColumn(rs);
+ }
}
finally {
- if (rs != null) rs.close();
+ if (rs != null) {
+ rs.close();
+ }
}
}
Show replies by date