From 1c556dc775708706b6ca84251cb170d3c46f729f Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Wed, 28 Apr 2010 04:32:00 +0000 Subject: [PATCH] HHH-4991 ; ManyToMany table not joined due to max_fetch_depth parameter, results to SQL exceptions git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19312 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../ManyToManyMaxFetchDepth0Test.java | 48 ++++++++ .../manytomany/ManyToManyTest.java | 24 ++++ .../java/org/hibernate/loader/JoinWalker.java | 2 +- .../loader/criteria/CriteriaJoinWalker.java | 26 ++++- .../test/legacy/ParentChildTest.java | 106 +++++++++++++++++- 5 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java diff --git a/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java b/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java new file mode 100644 index 0000000000..0c3be1424f --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java @@ -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 diff --git a/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java b/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java index 5adbb8a6f8..cb1727d29c 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java @@ -15,6 +15,7 @@ import org.hibernate.Hibernate; 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 @@ public class ManyToManyTest extends TestCase { 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() ); + fnac.setSuppliers( new HashSet() ); + 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; diff --git a/core/src/main/java/org/hibernate/loader/JoinWalker.java b/core/src/main/java/org/hibernate/loader/JoinWalker.java index fc2b35ae5a..a383cdc71b 100755 --- a/core/src/main/java/org/hibernate/loader/JoinWalker.java +++ b/core/src/main/java/org/hibernate/loader/JoinWalker.java @@ -473,7 +473,7 @@ public class JoinWalker { * {@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, diff --git a/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java b/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java index decd822a40..2316243106 100755 --- a/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java +++ b/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java @@ -170,7 +170,31 @@ public class CriteriaJoinWalker extends AbstractEntityJoinWalker { } } } - + + 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; } diff --git a/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java b/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java index e6f148bee7..5dcc418562 100644 --- a/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java +++ b/testsuite/src/test/java/org/hibernate/test/legacy/ParentChildTest.java @@ -31,6 +31,7 @@ import org.hibernate.dialect.MySQLDialect; 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 @@ public class ParentChildTest extends LegacyTestCase { 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 @@ public class ParentChildTest extends LegacyTestCase { 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();