[hibernate-commits] Hibernate SVN: r18743 - in branches/Branch_3_2/HibernateExt/tools: src/java/org/hibernate/cfg and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Feb 9 07:38:48 EST 2010


Author: max.andersen at jboss.com
Date: 2010-02-09 07:38:47 -0500 (Tue, 09 Feb 2010)
New Revision: 18743

Added:
   branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/TernarySchemaTest.java
Modified:
   branches/Branch_3_2/HibernateExt/tools/.classpath
   branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/JDBCBinder.java
   branches/Branch_3_2/HibernateExt/tools/src/templates/hbm/set.hbm.ftl
Log:
Fix JBIDE-5628, <set> now insert catalog/schema if different from parent entity.

Modified: branches/Branch_3_2/HibernateExt/tools/.classpath
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/.classpath	2010-02-09 12:16:09 UTC (rev 18742)
+++ branches/Branch_3_2/HibernateExt/tools/.classpath	2010-02-09 12:38:47 UTC (rev 18743)
@@ -27,7 +27,6 @@
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/ant-1.6.5.jar"/>
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/cglib-2.1.3.jar"/>
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/junit-3.8.1.jar"/>
-	<classpathentry kind="lib" path="/hibernate-3.2/jdbc/hsqldb.jar"/>
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/ant-antlr-1.6.5.jar"/>
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/ant-junit-1.6.5.jar"/>
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/ant-launcher-1.6.5.jar"/>
@@ -51,7 +50,6 @@
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/jboss-system.jar"/>
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/jgroups-2.2.8.jar"/>
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/jta.jar"/>
-	<classpathentry kind="lib" path="/hibernate-3.2/lib/log4j-1.2.11.jar"/>
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/oscache-2.1.jar"/>
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/proxool-0.8.3.jar"/>
 	<classpathentry kind="lib" path="/hibernate-3.2/lib/swarmcache-1.0rc2.jar"/>
@@ -63,5 +61,7 @@
 	<classpathentry kind="lib" path="C:/work/products/slf4j-1.5.10/slf4j-log4j12-1.5.10.jar"/>
 	<classpathentry kind="lib" path="C:/work/products/hibernate-distribution-3.3.2.GA/lib/required/commons-collections-3.1.jar"/>
 	<classpathentry kind="lib" path="C:/work/products/hibernate-distribution-3.3.2.GA/hibernate3.jar" sourcepath="C:/work/products/hibernate-distribution-3.3.2.GA/project"/>
-	<classpathentry kind="output" path="build/classes"/>
+	<classpathentry kind="lib" path="C:/work/products/apache-log4j-1.2.15/log4j-1.2.15.jar"/>
+	<classpathentry kind="lib" path="lib/jdbc/hsqldb.jar"/>
+	<classpathentry kind="output" path="build/eclipseclasses"/>
 </classpath>

Modified: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/JDBCBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/JDBCBinder.java	2010-02-09 12:16:09 UTC (rev 18742)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/JDBCBinder.java	2010-02-09 12:38:47 UTC (rev 18743)
@@ -150,9 +150,11 @@
 				continue;
 			}
 
+	    	
 			RootClass rc = new RootClass();
 			TableIdentifier tableIdentifier = TableIdentifier.create(table);
 			String className = revengStrategy.tableToClassName( tableIdentifier );
+			log.debug("Building entity " + className + " based on " + tableIdentifier);
 			rc.setEntityName( className );
 			rc.setClassName( className );
 			rc.setProxyInterfaceName( rc.getEntityName() ); // TODO: configurable ?

Modified: branches/Branch_3_2/HibernateExt/tools/src/templates/hbm/set.hbm.ftl
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/templates/hbm/set.hbm.ftl	2010-02-09 12:16:09 UTC (rev 18742)
+++ branches/Branch_3_2/HibernateExt/tools/src/templates/hbm/set.hbm.ftl	2010-02-09 12:38:47 UTC (rev 18743)
@@ -7,6 +7,12 @@
 	inverse="${value.inverse?string}"
 	lazy="${c2h.getCollectionLazy(value)}" 
 	table="${value.collectionTable.name}"
+	<#if value.collectionTable.catalog?exists && ((clazz.table.catalog?exists && clazz.table.catalog!=value.collectionTable.catalog) || (!clazz.table.catalog?exists && value.collectionTable.catalog?exists)) >
+    catalog="${value.collectionTable.catalog}"
+	</#if>
+	<#if value.collectionTable.schema?exists && ((clazz.table.schema?exists && clazz.table.schema!=value.collectionTable.schema) || (!clazz.table.schema?exists && value.collectionTable.schema?exists)) >
+    schema="${value.collectionTable.quotedSchema}"
+	</#if>	
 	<#if property.cascade != "none">
         cascade="${property.cascade}"
 	</#if>

Added: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/TernarySchemaTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/TernarySchemaTest.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/TernarySchemaTest.java	2010-02-09 12:38:47 UTC (rev 18743)
@@ -0,0 +1,159 @@
+/*
+ * Created on 2004-11-23
+ *
+ */
+package org.hibernate.tool.test.jdbc2cfg;
+
+import java.io.File;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.JDBCMetaDataConfiguration;
+import org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy;
+import org.hibernate.cfg.reveng.SchemaSelection;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Property;
+import org.hibernate.mapping.Set;
+import org.hibernate.tool.JDBCMetaDataBinderTestCase;
+import org.hibernate.tool.hbm2x.HibernateMappingExporter;
+import org.hibernate.tool.hbm2x.visitor.DefaultValueVisitor;
+
+/**
+ * 
+ * Tests multi schema used in collections (set)'s. See JBIDE-5628.
+ * 
+ * Excluded from default tests for now since it currently requires HSQLDB 2.0 since previous versions of hsqldb does not support cross-schema foreign key checks.
+ * 
+ * @author max
+ * 
+ */
+public class TernarySchemaTest extends JDBCMetaDataBinderTestCase {
+
+	/**
+	 * @return
+	 */
+	protected String[] getDropSQL() {
+		return new String[] { 
+				"drop table plainuserroles",
+				"drop table plainrole",
+				"drop table thirdschema.userroles",
+				"drop table user",
+				"drop table otherschema.role",				
+				"drop schema otherschema",
+				"drop schema thirdschema"};
+	}
+
+	/**
+	 * @return
+	 */
+	protected String[] getCreateSQL() {
+
+		return new String[] {
+				"create schema otherschema authorization dba",
+				"create schema thirdschema authorization dba",
+				
+				"create table user ( id int not null, name varchar(20), primary key(id))",
+				"create table otherschema.role ( id int not null, name varchar(20), primary key(id))",				
+				"create table thirdschema.userroles ( userid int not null, roleid int not null, primary key(userid, roleid))",
+				"alter table thirdschema.userroles add constraint toroles foreign key (roleid) references otherschema.role(id)",
+				"alter table thirdschema.userroles add constraint tousers foreign key (userid) references public.user(id)",				
+				
+				"create table plainrole ( id int not null, name varchar(20), primary key(id))",				
+				"create table plainuserroles ( userid int not null, roleid int not null, primary key(userid, roleid))",
+				"alter table plainuserroles add constraint plaintoroles foreign key (roleid) references plainrole(id)",
+				"alter table plainuserroles add constraint plaintousers foreign key (userid) references user(id)",				
+				
+		};
+	}
+
+	public void testTernaryModel() throws SQLException {
+
+		assertMultiSchema(getConfiguration());
+		
+	}
+
+	private void assertMultiSchema(Configuration cfg) {
+		assertHasNext("There should be three tables!", 5, cfg
+				.getTableMappings());
+
+		final PersistentClass role = cfg.getClassMapping("Role");
+		PersistentClass userroles = cfg.getClassMapping("Userroles");
+		PersistentClass user = cfg.getClassMapping("User");
+		PersistentClass plainRole = cfg.getClassMapping("Plainrole");
+		
+
+		Property property = role.getProperty("users");
+		assertEquals(role.getTable().getSchema(), "OTHERSCHEMA");
+		assertNotNull(property);
+		property.getValue().accept(new DefaultValueVisitor(true) {
+			public Object accept(Set o) {
+				assertEquals(o.getCollectionTable().getSchema(), "THIRDSCHEMA");
+				return null;
+			}
+		});
+		
+		
+
+		property = plainRole.getProperty("users");
+		assertEquals(role.getTable().getSchema(), "OTHERSCHEMA");
+		assertNotNull(property);
+		property.getValue().accept(new DefaultValueVisitor(true) {
+			public Object accept(Set o) {
+				assertEquals(o.getCollectionTable().getSchema(), null);
+				return null;
+			}
+		});
+
+	}
+	
+	public void testGeneration() {
+		
+	
+		cfg.buildMappings();
+		
+		HibernateMappingExporter hme = new HibernateMappingExporter(cfg, getOutputDir());
+		hme.start();		
+		
+		assertFileAndExists( new File(getOutputDir(), "Role.hbm.xml") );
+		assertFileAndExists( new File(getOutputDir(), "User.hbm.xml") );
+		assertFileAndExists( new File(getOutputDir(), "Plainrole.hbm.xml") );
+		
+		assertEquals(3, getOutputDir().listFiles().length);
+		
+		Configuration configuration = new Configuration()
+		    .addFile( new File(getOutputDir(), "Role.hbm.xml") )
+		    .addFile( new File(getOutputDir(), "User.hbm.xml") )
+		    .addFile( new File(getOutputDir(), "Plainrole.hbm.xml"));
+		
+		configuration.buildMappings();
+		
+		assertMultiSchema(configuration);
+	}
+	
+	protected void configure(JDBCMetaDataConfiguration configuration) {
+		
+		super.configure(configuration);
+		 DefaultReverseEngineeringStrategy c = new DefaultReverseEngineeringStrategy() {
+			 public List getSchemaSelections() {
+				 List selections = new ArrayList();
+				 selections.add(new SchemaSelection(null, "PUBLIC"));
+				 selections.add(new SchemaSelection(null, "otherschema"));
+				 selections.add(new SchemaSelection(null, "thirdschema"));
+				return selections;
+			}
+		 };
+	             
+	     configuration.setReverseEngineeringStrategy(c);
+	}
+
+	
+	public static Test suite() {
+		return new TestSuite( TernarySchemaTest.class );
+	}
+
+}



More information about the hibernate-commits mailing list