diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/SelectClause.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/SelectClause.java index 15873916c7..1624155587 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/SelectClause.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/SelectClause.java @@ -1,10 +1,10 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * Copyright (c) 2008, 2013, Red Hat Inc. 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. + * distributed under license by Red Hat Inc. * * 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 @@ -20,7 +20,6 @@ * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA - * */ package org.hibernate.hql.internal.ast.tree; import java.util.ArrayList; @@ -139,7 +138,7 @@ public class SelectClause extends SelectExpressionList { // NOTE: This must be done *before* invoking setScalarColumnText() because setScalarColumnText() // changes the AST!!! SelectExpression[] selectExpressions = collectSelectExpressions(); - + for ( int i = 0; i < selectExpressions.length; i++ ) { SelectExpression selectExpression = selectExpressions[i]; @@ -176,14 +175,14 @@ public class SelectClause extends SelectExpressionList { if ( !getWalker().isShallowQuery() ) { // add the fetched entities List fromElements = fromClause.getProjectionList(); - + ASTAppender appender = new ASTAppender( getASTFactory(), this ); // Get ready to start adding nodes. int size = fromElements.size(); Iterator iterator = fromElements.iterator(); for ( int k = 0; iterator.hasNext(); k++ ) { FromElement fromElement = ( FromElement ) iterator.next(); - + if ( fromElement.isFetch() ) { FromElement origin = null; if ( fromElement.getRealOrigin() == null ) { @@ -226,7 +225,7 @@ public class SelectClause extends SelectExpressionList { } } } - + // generate id select fragment and then property select fragment for // each expression, just like generateSelectFragments(). renderNonScalarSelects( collectSelectExpressions(), fromClause ); @@ -326,7 +325,7 @@ public class SelectClause extends SelectExpressionList { private void addCollectionFromElement(FromElement fromElement) { if ( fromElement.isFetch() ) { - if ( fromElement.isCollectionJoin() || fromElement.getQueryableCollection() != null ) { + if ( fromElement.getQueryableCollection() != null ) { String suffix; if (collectionFromElements==null) { collectionFromElements = new ArrayList(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/Contact.java b/hibernate-core/src/test/java/org/hibernate/test/collection/basic/Contact.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/Contact.java rename to hibernate-core/src/test/java/org/hibernate/test/collection/basic/Contact.java index cb63418055..7706fe2225 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/Contact.java +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/basic/Contact.java @@ -1,4 +1,4 @@ -package org.hibernate.test.collection.set.hhh8206; +package org.hibernate.test.collection.basic; import java.io.Serializable; import java.util.HashSet; @@ -7,13 +7,10 @@ import javax.persistence.Basic; import javax.persistence.CollectionTable; import javax.persistence.ElementCollection; import javax.persistence.Entity; -import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; -import javax.persistence.ManyToMany; -import javax.persistence.OneToMany; import javax.persistence.Table; @Entity diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/EmailAddress.java b/hibernate-core/src/test/java/org/hibernate/test/collection/basic/EmailAddress.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/EmailAddress.java rename to hibernate-core/src/test/java/org/hibernate/test/collection/basic/EmailAddress.java index 50fcf5a0dc..5611b868c6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/EmailAddress.java +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/basic/EmailAddress.java @@ -1,7 +1,6 @@ -package org.hibernate.test.collection.set.hhh8206; +package org.hibernate.test.collection.basic; import java.io.Serializable; -import java.util.Set; import javax.persistence.*; @Embeddable diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/basic/JoinFetchElementCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/test/collection/basic/JoinFetchElementCollectionTest.java new file mode 100644 index 0000000000..f728aeb9df --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/basic/JoinFetchElementCollectionTest.java @@ -0,0 +1,133 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. 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 Inc. + * + * 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.collection.basic; + +import java.util.HashSet; +import java.util.Set; + +import org.hibernate.Session; + +import org.junit.Assert; +import org.junit.Test; + +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +public class JoinFetchElementCollectionTest extends BaseCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {Contact.class, EmailAddress.class, User.class}; + } + + @Test + @TestForIssue(jiraKey = "HHH-8206") + @FailureExpected(jiraKey = "HHH-8206", message = "This is not explicitly supported, however should arguably throw an exception") + public void testJoinFetchesByPath() { + Set emailAddresses = new HashSet(); + emailAddresses.add( new EmailAddress( "test1@test.com" ) ); + emailAddresses.add( new EmailAddress( "test2@test.com" ) ); + emailAddresses.add( new EmailAddress( "test3@test.com" ) ); + + { + // Session 1: Insert a user with email addresses but no emailAddresses2 + Session session = openSession(); + session.beginTransaction(); + + User user = new User(); + user.setName( "john" ); + Contact contact = new Contact(); + contact.setName( "John Doe" ); + contact.setEmailAddresses( emailAddresses ); + contact = (Contact) session.merge( contact ); + user.setContact( contact ); + user = (User) session.merge( user ); + + session.getTransaction().commit(); + session.close(); + } + { + // Session 2: Retrieve the user object and check if the sets have the expected values + Session session = openSession(); + session.beginTransaction(); + final String qry = "SELECT user " + + "FROM User user " + + "LEFT OUTER JOIN FETCH user.contact " + + "LEFT OUTER JOIN FETCH user.contact.emailAddresses2 " + + "LEFT OUTER JOIN FETCH user.contact.emailAddresses"; + User user = (User) session.createQuery( qry ).uniqueResult(); + session.getTransaction().commit(); + session.close(); + + Assert.assertEquals( emailAddresses, user.getContact().getEmailAddresses() ); + Assert.assertTrue( user.getContact().getEmailAddresses2().isEmpty() ); + } + + } + + @Test + @TestForIssue(jiraKey = "HHH-5465") + public void testJoinFetchElementCollection() { + Set emailAddresses = new HashSet(); + emailAddresses.add( new EmailAddress( "test1@test.com" ) ); + emailAddresses.add( new EmailAddress( "test2@test.com" ) ); + emailAddresses.add( new EmailAddress( "test3@test.com" ) ); + + { + // Session 1: Insert a user with email addresses but no emailAddresses2 + Session session = openSession(); + session.beginTransaction(); + + User user = new User(); + user.setName( "john" ); + Contact contact = new Contact(); + contact.setName( "John Doe" ); + contact.setEmailAddresses( emailAddresses ); + contact = (Contact) session.merge( contact ); + user.setContact( contact ); + user = (User) session.merge( user ); + + session.getTransaction().commit(); + session.close(); + } + { + // Session 2: Retrieve the user object and check if the sets have the expected values + Session session = openSession(); + session.beginTransaction(); + final String qry = "SELECT user " + + "FROM User user " + + "LEFT OUTER JOIN FETCH user.contact c " + + "LEFT OUTER JOIN FETCH c.emailAddresses2 " + + "LEFT OUTER JOIN FETCH c.emailAddresses"; + User user = (User) session.createQuery( qry ).uniqueResult(); + session.getTransaction().commit(); + session.close(); + + Assert.assertEquals( emailAddresses, user.getContact().getEmailAddresses() ); + Assert.assertTrue( user.getContact().getEmailAddresses2().isEmpty() ); + } + + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/User.java b/hibernate-core/src/test/java/org/hibernate/test/collection/basic/User.java similarity index 85% rename from hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/User.java rename to hibernate-core/src/test/java/org/hibernate/test/collection/basic/User.java index 315195a168..ab244a4a31 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/User.java +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/basic/User.java @@ -1,20 +1,13 @@ -package org.hibernate.test.collection.set.hhh8206; +package org.hibernate.test.collection.basic; import java.io.Serializable; -import java.util.HashSet; -import java.util.Set; import javax.persistence.Basic; -import javax.persistence.CollectionTable; -import javax.persistence.ElementCollection; import javax.persistence.Entity; -import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; -import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; import javax.persistence.Table; @Entity diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/JoinFetchElementCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/JoinFetchElementCollectionTest.java deleted file mode 100644 index 2870a88517..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/set/hhh8206/JoinFetchElementCollectionTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.hibernate.test.collection.set.hhh8206; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Set; - -import org.hibernate.Session; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Test; - -public class JoinFetchElementCollectionTest extends BaseCoreFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] {Contact.class, EmailAddress.class, User.class}; - } - - @Test - public void test(){ - Set emailAddresses = new HashSet(); - emailAddresses.add(new EmailAddress("test1@test.com")); - emailAddresses.add(new EmailAddress("test2@test.com")); - emailAddresses.add(new EmailAddress("test3@test.com")); - - { - // Session 1: Insert a user with email addresses but no emailAddresses2 - Session session = openSession(); - session.beginTransaction(); - - User user = new User(); - user.setName("john"); - Contact contact = new Contact(); - contact.setName("John Doe"); - contact.setEmailAddresses(emailAddresses); - contact = (Contact) session.merge(contact); - user.setContact(contact); - user = (User) session.merge(user); - - session.getTransaction().commit(); - session.close(); - } - { - // Session 2: Retrieve the user object and check if the sets have the expected values - Session session = openSession(); - session.beginTransaction(); - User user = (User) session.createQuery("SELECT user " - + "FROM User user " - + "LEFT OUTER JOIN FETCH user.contact " - + "LEFT OUTER JOIN FETCH user.contact.emailAddresses2 " - + "LEFT OUTER JOIN FETCH user.contact.emailAddresses") - .uniqueResult(); - session.getTransaction().commit(); - session.close(); - - Assert.assertEquals(emailAddresses, user.getContact().getEmailAddresses()); - Assert.assertTrue(user.getContact().getEmailAddresses2().isEmpty()); - } - - } - -}