HHH-12314 - Fix ClassCastException when using a NegatedPredicateWrapper on a Criteria Join.

This commit is contained in:
Chris Cranford 2018-03-16 13:32:24 -04:00
parent b0cd713ac7
commit 748c521d71
4 changed files with 138 additions and 3 deletions

View File

@ -19,7 +19,7 @@ import org.hibernate.query.criteria.internal.FromImplementor;
import org.hibernate.query.criteria.internal.JoinImplementor; import org.hibernate.query.criteria.internal.JoinImplementor;
import org.hibernate.query.criteria.internal.PathSource; import org.hibernate.query.criteria.internal.PathSource;
import org.hibernate.query.criteria.internal.compile.RenderingContext; import org.hibernate.query.criteria.internal.compile.RenderingContext;
import org.hibernate.query.criteria.internal.predicate.AbstractPredicateImpl; import org.hibernate.query.criteria.internal.predicate.PredicateImplementor;
/** /**
* Convenience base class for various {@link javax.persistence.criteria.Join} implementations. * Convenience base class for various {@link javax.persistence.criteria.Join} implementations.
@ -83,7 +83,7 @@ public abstract class AbstractJoinImpl<Z, X>
.append( getAlias() ); .append( getAlias() );
if ( suppliedJoinCondition != null ) { if ( suppliedJoinCondition != null ) {
tableExpression.append( " with " ) tableExpression.append( " with " )
.append( ( (AbstractPredicateImpl) suppliedJoinCondition ).render( renderingContext ) ); .append( ( (PredicateImplementor) suppliedJoinCondition ).render( renderingContext ) );
} }
return tableExpression.toString(); return tableExpression.toString();
} }

View File

@ -0,0 +1,52 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.criteria;
import java.util.List;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
/**
* @author Chris Cranford
*/
@Entity
public class Book {
@Id
@GeneratedValue
private Integer id;
@OneToMany
private Set<Store> stores;
public Book() {
}
public Book(Set<Store> stores) {
this.stores = stores;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set<Store> getStores() {
return stores;
}
public void setStores(Set<Store> stores) {
this.stores = stores;
}
}

View File

@ -7,13 +7,16 @@
package org.hibernate.jpa.test.criteria; package org.hibernate.jpa.test.criteria;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.List; import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;
import javax.persistence.metamodel.EntityType; import javax.persistence.metamodel.EntityType;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
@ -62,7 +65,9 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
Phone.class, Phone.class,
Product.class, Product.class,
ShelfLife.class, ShelfLife.class,
Spouse.class Spouse.class,
Book.class,
Store.class
}; };
} }
@ -311,4 +316,35 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
em.createQuery( criteria ).getResultList(); em.createQuery( criteria ).getResultList();
} ); } );
} }
@Test
@TestForIssue(jiraKey = "HHH-12314")
public void testJoinUsingNegatedPredicate() {
// Write test data
doInJPA( this::entityManagerFactory, entityManager -> {
final Store store = new Store();
store.setName( "Acme Books" );
store.setAddress( "123 Main St" );
entityManager.persist( store );
final Book book = new Book();
book.setStores( new HashSet<>( Arrays.asList( store ) ) );
entityManager.persist( book );
} );
doInJPA( this::entityManagerFactory, entityManager -> {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Book> query = cb.createQuery( Book.class );
final Root<Book> bookRoot = query.from( Book.class );
SetJoin<Book, Store> storeJoin = bookRoot.join( Book_.stores );
storeJoin.on( cb.isNotNull( storeJoin.get( Store_.address ) ) );
// Previously failed due to ClassCastException
// org.hibernate.query.criteria.internal.predicate.NegatedPredicateWrapper
// cannot be cast to
// org.hibernate.query.criteria.internal.predicate.AbstractPredicateImpl
entityManager.createQuery( query ).getResultList();
} );
}
} }

View File

@ -0,0 +1,47 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.criteria;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Chris Cranford
*/
@Entity
public class Store {
@Id
@GeneratedValue
private Integer id;
private String name;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}