HHH-5465 - HQL left join fetch of an element collection following a left join fetch of a one-to-one relationship causes NullPointerException

This commit is contained in:
Steve Ebersole 2013-05-07 14:13:54 -05:00 committed by Brett Meyer
parent 33268fe0dd
commit f7653aa7e3
6 changed files with 143 additions and 83 deletions

View File

@ -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;
@ -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();

View File

@ -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

View File

@ -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

View File

@ -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<EmailAddress> emailAddresses = new HashSet<EmailAddress>();
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<EmailAddress> emailAddresses = new HashSet<EmailAddress>();
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() );
}
}
}

View File

@ -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

View File

@ -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<EmailAddress> emailAddresses = new HashSet<EmailAddress>();
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());
}
}
}