From 6713d24d6b49bcbcb603271350b66618dae4561b Mon Sep 17 00:00:00 2001 From: Didier Villevalois Date: Thu, 1 Mar 2012 15:44:38 +0100 Subject: [PATCH] HHH-7134 - Detection of wrong circularity when two tables with same name in different schemas Fixes FKSecondPass processing to take catalog and schema in accounts. Corresponding test class is org.hibernate.test.cfg.WrongCircularityDetectionTest. --- .../java/org/hibernate/cfg/Configuration.java | 8 +- .../cfg/WrongCircularityDetectionTest.java | 74 +++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/cfg/WrongCircularityDetectionTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java index a1b50523ba..6245908849 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java @@ -1421,7 +1421,7 @@ public class Configuration implements Serializable { if ( sp.isInPrimaryKey() ) { String referenceEntityName = sp.getReferencedEntityName(); PersistentClass classMapping = getClassMapping( referenceEntityName ); - String dependentTable = classMapping.getTable().getQuotedName(); + String dependentTable = quotedTableName(classMapping.getTable()); if ( !isADependencyOf.containsKey( dependentTable ) ) { isADependencyOf.put( dependentTable, new HashSet() ); } @@ -1491,7 +1491,7 @@ public class Configuration implements Serializable { } for ( FkSecondPass sp : dependencies ) { - String dependentTable = sp.getValue().getTable().getQuotedName(); + String dependentTable = quotedTableName(sp.getValue().getTable()); if ( dependentTable.compareTo( startTable ) == 0 ) { StringBuilder sb = new StringBuilder( "Foreign key circularity dependency involving the following tables: " @@ -1505,6 +1505,10 @@ public class Configuration implements Serializable { } } + private String quotedTableName(Table table) { + return Table.qualify( table.getCatalog(), table.getQuotedSchema(), table.getQuotedName() ); + } + private void processEndOfQueue(List endOfQueueFkSecondPasses) { /* * If a second pass raises a recoverableException, queue it for next round diff --git a/hibernate-core/src/test/java/org/hibernate/test/cfg/WrongCircularityDetectionTest.java b/hibernate-core/src/test/java/org/hibernate/test/cfg/WrongCircularityDetectionTest.java new file mode 100644 index 0000000000..6b66ce94f7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/cfg/WrongCircularityDetectionTest.java @@ -0,0 +1,74 @@ +package org.hibernate.test.cfg; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import javax.persistence.Basic; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import org.hibernate.cfg.Configuration; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +/** + * This test illustrates the problem when two related (in terms of joins) + * classes have the same table name in different schemas. + * + * @author Didier Villevalois + */ +@TestForIssue(jiraKey = "HHH-7134") +public class WrongCircularityDetectionTest extends BaseUnitTestCase { + + @Test + public void testNoCircularityDetection() { + Configuration cfg = new Configuration(); + cfg.addAnnotatedClass(Entity1.class); + cfg.addAnnotatedClass(Entity2.class); + + cfg.buildMappings(); + + org.hibernate.mapping.Table entity1Table = cfg.getClassMapping( + Entity1.class.getName()).getTable(); + org.hibernate.mapping.Table entity2Table = cfg.getClassMapping( + Entity2.class.getName()).getTable(); + + assertTrue(entity1Table.getName().equals(entity2Table.getName())); + assertFalse(entity1Table.getSchema().equals(entity2Table.getSchema())); + } + + @Entity + @Inheritance(strategy = InheritanceType.JOINED) + @Table(schema = "schema1", name = "entity") + public static class Entity1 { + private String id; + + @Id + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + } + + @Entity + @Table(schema = "schema2", name = "entity") + public static class Entity2 extends Entity1 { + private String value; + + @Basic + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + } +}