Author: steve.ebersole(a)jboss.com
Date: 2009-06-01 17:49:42 -0400 (Mon, 01 Jun 2009)
New Revision: 16657
Modified:
core/branches/Branch_3_3/core/src/main/java/org/hibernate/cfg/Configuration.java
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/TableMetadata.java
Log:
HHH-3532 - schema update task should look for foreign key signature
Modified:
core/branches/Branch_3_3/core/src/main/java/org/hibernate/cfg/Configuration.java
===================================================================
---
core/branches/Branch_3_3/core/src/main/java/org/hibernate/cfg/Configuration.java 2009-06-01
21:12:25 UTC (rev 16656)
+++
core/branches/Branch_3_3/core/src/main/java/org/hibernate/cfg/Configuration.java 2009-06-01
21:49:42 UTC (rev 16657)
@@ -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/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java
===================================================================
---
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java 2009-06-01
21:12:25 UTC (rev 16656)
+++
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java 2009-06-01
21:49:42 UTC (rev 16657)
@@ -26,33 +26,64 @@
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);
+ void addReference(ResultSet rs) throws SQLException {
+ references.put(rs.getString("FKCOLUMN_NAME").toLowerCase(),
+ rs.getString("PKCOLUMN_NAME"));
}
- public ColumnMetadata[] getColumns() {
- return (ColumnMetadata[]) columns.toArray(new ColumnMetadata[0]);
+ 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/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/TableMetadata.java
===================================================================
---
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/TableMetadata.java 2009-06-01
21:12:25 UTC (rev 16656)
+++
core/branches/Branch_3_3/core/src/main/java/org/hibernate/tool/hbm2ddl/TableMetadata.java 2009-06-01
21:49:42 UTC (rev 16657)
@@ -29,10 +29,13 @@
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
@@ -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() );
}
@@ -106,7 +120,7 @@
foreignKeys.put( info.getName().toLowerCase(), info );
}
- info.addColumn( getColumnMetadata( rs.getString("FKCOLUMN_NAME") ) );
+ info.addReference( rs );
}
private void addIndex(ResultSet rs) throws SQLException {
Show replies by date