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:
parent
33268fe0dd
commit
f7653aa7e3
|
@ -1,10 +1,10 @@
|
||||||
/*
|
/*
|
||||||
* Hibernate, Relational Persistence for Idiomatic Java
|
* 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
|
* indicated by the @author tags or express copyright attribution
|
||||||
* statements applied by the authors. All third-party contributions are
|
* 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,
|
* 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
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
@ -20,7 +20,6 @@
|
||||||
* Free Software Foundation, Inc.
|
* Free Software Foundation, Inc.
|
||||||
* 51 Franklin Street, Fifth Floor
|
* 51 Franklin Street, Fifth Floor
|
||||||
* Boston, MA 02110-1301 USA
|
* Boston, MA 02110-1301 USA
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
package org.hibernate.hql.internal.ast.tree;
|
package org.hibernate.hql.internal.ast.tree;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -139,7 +138,7 @@ public class SelectClause extends SelectExpressionList {
|
||||||
// NOTE: This must be done *before* invoking setScalarColumnText() because setScalarColumnText()
|
// NOTE: This must be done *before* invoking setScalarColumnText() because setScalarColumnText()
|
||||||
// changes the AST!!!
|
// changes the AST!!!
|
||||||
SelectExpression[] selectExpressions = collectSelectExpressions();
|
SelectExpression[] selectExpressions = collectSelectExpressions();
|
||||||
|
|
||||||
for ( int i = 0; i < selectExpressions.length; i++ ) {
|
for ( int i = 0; i < selectExpressions.length; i++ ) {
|
||||||
SelectExpression selectExpression = selectExpressions[i];
|
SelectExpression selectExpression = selectExpressions[i];
|
||||||
|
|
||||||
|
@ -176,14 +175,14 @@ public class SelectClause extends SelectExpressionList {
|
||||||
if ( !getWalker().isShallowQuery() ) {
|
if ( !getWalker().isShallowQuery() ) {
|
||||||
// add the fetched entities
|
// add the fetched entities
|
||||||
List fromElements = fromClause.getProjectionList();
|
List fromElements = fromClause.getProjectionList();
|
||||||
|
|
||||||
ASTAppender appender = new ASTAppender( getASTFactory(), this ); // Get ready to start adding nodes.
|
ASTAppender appender = new ASTAppender( getASTFactory(), this ); // Get ready to start adding nodes.
|
||||||
int size = fromElements.size();
|
int size = fromElements.size();
|
||||||
|
|
||||||
Iterator iterator = fromElements.iterator();
|
Iterator iterator = fromElements.iterator();
|
||||||
for ( int k = 0; iterator.hasNext(); k++ ) {
|
for ( int k = 0; iterator.hasNext(); k++ ) {
|
||||||
FromElement fromElement = ( FromElement ) iterator.next();
|
FromElement fromElement = ( FromElement ) iterator.next();
|
||||||
|
|
||||||
if ( fromElement.isFetch() ) {
|
if ( fromElement.isFetch() ) {
|
||||||
FromElement origin = null;
|
FromElement origin = null;
|
||||||
if ( fromElement.getRealOrigin() == null ) {
|
if ( fromElement.getRealOrigin() == null ) {
|
||||||
|
@ -226,7 +225,7 @@ public class SelectClause extends SelectExpressionList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate id select fragment and then property select fragment for
|
// generate id select fragment and then property select fragment for
|
||||||
// each expression, just like generateSelectFragments().
|
// each expression, just like generateSelectFragments().
|
||||||
renderNonScalarSelects( collectSelectExpressions(), fromClause );
|
renderNonScalarSelects( collectSelectExpressions(), fromClause );
|
||||||
|
@ -326,7 +325,7 @@ public class SelectClause extends SelectExpressionList {
|
||||||
|
|
||||||
private void addCollectionFromElement(FromElement fromElement) {
|
private void addCollectionFromElement(FromElement fromElement) {
|
||||||
if ( fromElement.isFetch() ) {
|
if ( fromElement.isFetch() ) {
|
||||||
if ( fromElement.isCollectionJoin() || fromElement.getQueryableCollection() != null ) {
|
if ( fromElement.getQueryableCollection() != null ) {
|
||||||
String suffix;
|
String suffix;
|
||||||
if (collectionFromElements==null) {
|
if (collectionFromElements==null) {
|
||||||
collectionFromElements = new ArrayList();
|
collectionFromElements = new ArrayList();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package org.hibernate.test.collection.set.hhh8206;
|
package org.hibernate.test.collection.basic;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -7,13 +7,10 @@ import javax.persistence.Basic;
|
||||||
import javax.persistence.CollectionTable;
|
import javax.persistence.CollectionTable;
|
||||||
import javax.persistence.ElementCollection;
|
import javax.persistence.ElementCollection;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.FetchType;
|
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToMany;
|
|
||||||
import javax.persistence.OneToMany;
|
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
|
@ -1,7 +1,6 @@
|
||||||
package org.hibernate.test.collection.set.hhh8206;
|
package org.hibernate.test.collection.basic;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Set;
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
|
||||||
@Embeddable
|
@Embeddable
|
|
@ -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() );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,20 +1,13 @@
|
||||||
package org.hibernate.test.collection.set.hhh8206;
|
package org.hibernate.test.collection.basic;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
import javax.persistence.Basic;
|
import javax.persistence.Basic;
|
||||||
import javax.persistence.CollectionTable;
|
|
||||||
import javax.persistence.ElementCollection;
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.FetchType;
|
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToMany;
|
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
import javax.persistence.OneToMany;
|
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
|
@ -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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue