Author: gbadner
Date: 2010-04-27 19:55:49 -0400 (Tue, 27 Apr 2010)
New Revision: 19311
Added:
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java
Modified:
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java
core/branches/Branch_3_5/core/src/main/java/org/hibernate/loader/JoinWalker.java
core/branches/Branch_3_5/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java
Log:
HHH-4991 ; ManyToMany table not joined due to max_fetch_depth parameter, results to SQL
exceptions
Added:
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java
===================================================================
---
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java
(rev 0)
+++
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java 2010-04-27
23:55:49 UTC (rev 19311)
@@ -0,0 +1,48 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ *
+ */
+package org.hibernate.test.annotations.manytomany;
+
+
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+
+/**
+ * Many to many tests using max_fetch_depth == 0
+ *
+ * @author Gail Badner
+ */
+@SuppressWarnings("unchecked")
+public class ManyToManyMaxFetchDepth0Test extends ManyToManyTest {
+
+ public ManyToManyMaxFetchDepth0Test(String x) {
+ super( x );
+ }
+
+ @Override
+ protected void configure(Configuration cfg) {
+ cfg.setProperty( Environment.MAX_FETCH_DEPTH, "0" );
+ super.configure( cfg );
+ }
+}
\ No newline at end of file
Modified:
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java
===================================================================
---
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java 2010-04-27
22:51:17 UTC (rev 19310)
+++
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java 2010-04-27
23:55:49 UTC (rev 19311)
@@ -15,6 +15,7 @@
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.Transaction;
+import org.hibernate.criterion.Restrictions;
import org.hibernate.test.annotations.TestCase;
/**
@@ -80,6 +81,29 @@
s.close();
}
+ public void testCanUseCriteriaQuery() throws Exception {
+ Session s;
+ Transaction tx;
+ s = openSession();
+ tx = s.beginTransaction();
+ Store fnac = new Store();
+ fnac.setName( "Fnac" );
+ Supplier emi = new Supplier();
+ emi.setName( "Emmanuel" );
+ emi.setSuppStores( new HashSet<Store>() );
+ fnac.setSuppliers( new HashSet<Supplier>() );
+ fnac.getSuppliers().add( emi );
+ emi.getSuppStores().add( fnac );
+ s.persist( fnac );
+ tx.commit();
+ s.close();
+
+ s = openSession();
+ List result = s.createCriteria( Supplier.class ).createAlias( "suppStores",
"s" ).add(
+ Restrictions.eq( "s.name", "Fnac" ) ).list();
+ assertEquals( 1, result.size() );
+ s.close();
+ }
public void testDefaultCompositePk() throws Exception {
Session s;
Transaction tx;
Modified:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/loader/JoinWalker.java
===================================================================
---
core/branches/Branch_3_5/core/src/main/java/org/hibernate/loader/JoinWalker.java 2010-04-27
22:51:17 UTC (rev 19310)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/loader/JoinWalker.java 2010-04-27
23:55:49 UTC (rev 19311)
@@ -473,7 +473,7 @@
* {@link JoinFragment#LEFT_OUTER_JOIN}, or -1 to indicate no joining.
* @throws MappingException ??
*/
- private int getJoinType(
+ protected int getJoinType(
AssociationType associationType,
FetchMode config,
String path,
Modified:
core/branches/Branch_3_5/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java
===================================================================
---
core/branches/Branch_3_5/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java 2010-04-27
22:51:17 UTC (rev 19310)
+++
core/branches/Branch_3_5/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java 2010-04-27
23:55:49 UTC (rev 19311)
@@ -170,7 +170,31 @@
}
}
}
-
+
+ protected int getJoinType(
+ AssociationType associationType,
+ FetchMode config,
+ String path,
+ String lhsTable,
+ String[] lhsColumns,
+ boolean nullable,
+ int currentDepth,
+ CascadeStyle cascadeStyle) throws MappingException {
+ return ( translator.isJoin( path ) ?
+ translator.getJoinType( path ) :
+ super.getJoinType(
+ associationType,
+ config,
+ path,
+ lhsTable,
+ lhsColumns,
+ nullable,
+ currentDepth,
+ cascadeStyle
+ )
+ );
+ }
+
private static boolean isDefaultFetchMode(FetchMode fetchMode) {
return fetchMode==null || fetchMode==FetchMode.DEFAULT;
}
Modified:
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java
===================================================================
---
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java 2010-04-27
22:51:17 UTC (rev 19310)
+++
core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java 2010-04-27
23:55:49 UTC (rev 19311)
@@ -31,6 +31,7 @@
import org.hibernate.engine.EntityEntry;
import org.hibernate.impl.SessionImpl;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+import org.hibernate.proxy.HibernateProxy;
public class ParentChildTest extends LegacyTestCase {
@@ -207,7 +208,7 @@
s.close();
}
- public void testComplexCriteriaFailureExpected() throws Exception {
+ public void testComplexCriteria() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Baz baz = new Baz();
@@ -338,6 +339,109 @@
s.close();
}
+ public void testArrayHQL() {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ Baz baz = new Baz();
+ s.save(baz);
+ Foo foo1 = new Foo();
+ s.save(foo1);
+ baz.setFooArray( new FooProxy[] { foo1 } );
+
+ s.flush();
+ s.clear();
+
+ baz = ( Baz ) s.createQuery("from Baz b left join fetch
b.fooArray").uniqueResult();
+ assertEquals( 1, baz.getFooArray().length );
+
+ t.rollback();
+ s.close();
+
+ }
+
+ public void testArrayCriteria() {
+
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ Baz baz = new Baz();
+ s.save(baz);
+ Foo foo1 = new Foo();
+ s.save(foo1);
+ baz.setFooArray( new FooProxy[] { foo1 } );
+
+ s.flush();
+ s.clear();
+
+ baz = ( Baz ) s.createCriteria(Baz.class).createCriteria( "fooArray"
).uniqueResult();
+ assertEquals( 1, baz.getFooArray().length );
+
+ t.rollback();
+ s.close();
+ }
+
+ public void testLazyManyToOneHQL() {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ Baz baz = new Baz();
+ s.save(baz);
+ Foo foo1 = new Foo();
+ s.save(foo1);
+ baz.setFoo( foo1 );
+
+ s.flush();
+ s.clear();
+
+ baz = ( Baz ) s.createQuery("from Baz b").uniqueResult();
+ assertFalse( Hibernate.isInitialized( baz.getFoo() ) );
+ assertTrue( baz.getFoo() instanceof HibernateProxy );
+
+ t.rollback();
+ s.close();
+
+ }
+
+ public void testLazyManyToOneCriteria() {
+
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ Baz baz = new Baz();
+ s.save(baz);
+ Foo foo1 = new Foo();
+ s.save(foo1);
+ baz.setFoo( foo1 );
+
+ s.flush();
+ s.clear();
+
+ baz = ( Baz ) s.createCriteria( Baz.class ).uniqueResult();
+ assertTrue( Hibernate.isInitialized( baz.getFoo() ) );
+ assertFalse( baz.getFoo() instanceof HibernateProxy );
+
+ t.rollback();
+ s.close();
+ }
+
+ public void testLazyManyToOneGet() {
+
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ Baz baz = new Baz();
+ s.save(baz);
+ Foo foo1 = new Foo();
+ s.save(foo1);
+ baz.setFoo( foo1 );
+
+ s.flush();
+ s.clear();
+
+ baz = ( Baz ) s.get( Baz.class, baz.getCode() );
+ assertTrue( Hibernate.isInitialized( baz.getFoo() ) );
+ assertFalse( baz.getFoo() instanceof HibernateProxy );
+
+ t.rollback();
+ s.close();
+ }
+
public void testClassWhere() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();