Author: steve.ebersole(a)jboss.com
Date: 2009-06-01 17:50:00 -0400 (Mon, 01 Jun 2009)
New Revision: 16658
Modified:
core/branches/Branch_3_2/src/org/hibernate/cfg/Configuration.java
core/branches/Branch_3_2/src/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java
core/branches/Branch_3_2/src/org/hibernate/tool/hbm2ddl/TableMetadata.java
Log:
HHH-3532 - schema update task should look for foreign key signature
Modified: core/branches/Branch_3_2/src/org/hibernate/cfg/Configuration.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/cfg/Configuration.java 2009-06-01 21:49:42
UTC (rev 16657)
+++ core/branches/Branch_3_2/src/org/hibernate/cfg/Configuration.java 2009-06-01 21:50:00
UTC (rev 16658)
@@ -1020,7 +1020,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_2/src/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java
===================================================================
---
core/branches/Branch_3_2/src/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java 2009-06-01
21:49:42 UTC (rev 16657)
+++
core/branches/Branch_3_2/src/org/hibernate/tool/hbm2ddl/ForeignKeyMetadata.java 2009-06-01
21:50:00 UTC (rev 16658)
@@ -3,33 +3,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_2/src/org/hibernate/tool/hbm2ddl/TableMetadata.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/tool/hbm2ddl/TableMetadata.java 2009-06-01
21:49:42 UTC (rev 16657)
+++ core/branches/Branch_3_2/src/org/hibernate/tool/hbm2ddl/TableMetadata.java 2009-06-01
21:50:00 UTC (rev 16658)
@@ -5,8 +5,10 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.Map;
+import org.hibernate.mapping.ForeignKey;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -67,6 +69,17 @@
public ForeignKeyMetadata getForeignKeyMetadata(String keyName) {
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() );
@@ -83,7 +96,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