HHH-9073 - TREAT downcasting and result restrictions

This commit is contained in:
Steve Ebersole 2014-03-26 13:14:35 -05:00
parent 4a9057f31c
commit 4bb7966102
7 changed files with 88 additions and 5 deletions

View File

@ -139,12 +139,21 @@ public abstract class AbstractPathImpl<X>
SingularAttributePath<Y> path = (SingularAttributePath<Y>) resolveCachedAttributePath( attribute.getName() );
if ( path == null ) {
path = new SingularAttributePath<Y>( criteriaBuilder(), attribute.getJavaType(), this, attribute );
path = new SingularAttributePath<Y>(
criteriaBuilder(),
attribute.getJavaType(),
getPathSourceForSubPaths(),
attribute
);
registerAttributePath( attribute.getName(), path );
}
return path;
}
protected PathSource getPathSourceForSubPaths() {
return this;
}
@Override
@SuppressWarnings({ "unchecked" })
public <E, C extends Collection<E>> Expression<C> get(PluralAttribute<X, C, E> attribute) {

View File

@ -126,5 +126,15 @@ public class CollectionAttributeJoin<O,E>
public String render(RenderingContext renderingContext) {
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
}
@Override
public String getPathIdentifier() {
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
}
@Override
protected PathSource getPathSourceForSubPaths() {
return this;
}
}
}

View File

@ -134,5 +134,15 @@ public class ListAttributeJoin<O,E>
public String render(RenderingContext renderingContext) {
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
}
@Override
public String getPathIdentifier() {
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
}
@Override
protected PathSource getPathSourceForSubPaths() {
return this;
}
}
}

View File

@ -155,5 +155,15 @@ public class MapAttributeJoin<O,K,V>
public String render(RenderingContext renderingContext) {
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
}
@Override
public String getPathIdentifier() {
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
}
@Override
protected PathSource getPathSourceForSubPaths() {
return this;
}
}
}

View File

@ -30,6 +30,7 @@ import javax.persistence.metamodel.EntityType;
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
import org.hibernate.jpa.criteria.CriteriaSubqueryImpl;
import org.hibernate.jpa.criteria.FromImplementor;
import org.hibernate.jpa.criteria.PathSource;
import org.hibernate.jpa.criteria.compile.RenderingContext;
/**
@ -137,8 +138,22 @@ public class RootImpl<X> extends AbstractFromImpl<X,X> implements Root<X>, Seria
@Override
public String render(RenderingContext renderingContext) {
return getTreatFragment();
}
protected String getTreatFragment() {
return "treat(" + original.getAlias() + " as " + treatAsType.getName() + ")";
}
@Override
public String getPathIdentifier() {
return getTreatFragment();
}
@Override
protected PathSource getPathSourceForSubPaths() {
return this;
}
}
}

View File

@ -131,5 +131,15 @@ public class SetAttributeJoin<O,E>
public String render(RenderingContext renderingContext) {
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
}
@Override
public String getPathIdentifier() {
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
}
@Override
protected PathSource getPathSourceForSubPaths() {
return this;
}
}
}

View File

@ -23,22 +23,21 @@
*/
package org.hibernate.jpa.test.criteria;
import javax.persistence.*;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.EntityType;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.jpa.test.metamodel.Thing;
import org.hibernate.jpa.test.metamodel.ThingWithQuantity;
import org.hibernate.jpa.test.metamodel.ThingWithQuantity_;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
/**
* @author Steve Ebersole
*/
@ -110,6 +109,26 @@ public class TreatKeywordTest extends BaseEntityManagerFunctionalTestCase {
em.close();
}
@Test
public void treatPathClassTestHqlControl() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Animal animal = new Animal();
animal.setId(100L);
animal.setName("2");
em.persist(animal);
Human human = new Human();
human.setId(200L);
human.setName("2");
em.persist(human);
em.getTransaction().commit();
List<String> animalList = em.createQuery( "select a.name from Animal a where treat (a as Human).name like '2%'" ).getResultList();
Assert.assertEquals("treat(Animal as Human) was ignored",1, animalList.size());
em.close();
}
}