diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/AbstractPathImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/AbstractPathImpl.java index 8597fc9d4f..79057ce3e7 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/AbstractPathImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/AbstractPathImpl.java @@ -139,12 +139,21 @@ public abstract class AbstractPathImpl SingularAttributePath path = (SingularAttributePath) resolveCachedAttributePath( attribute.getName() ); if ( path == null ) { - path = new SingularAttributePath( criteriaBuilder(), attribute.getJavaType(), this, attribute ); + path = new SingularAttributePath( + criteriaBuilder(), + attribute.getJavaType(), + getPathSourceForSubPaths(), + attribute + ); registerAttributePath( attribute.getName(), path ); } return path; } + protected PathSource getPathSourceForSubPaths() { + return this; + } + @Override @SuppressWarnings({ "unchecked" }) public > Expression get(PluralAttribute attribute) { diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/CollectionAttributeJoin.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/CollectionAttributeJoin.java index 6e21730caf..12ba246f8a 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/CollectionAttributeJoin.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/CollectionAttributeJoin.java @@ -126,5 +126,15 @@ public class CollectionAttributeJoin 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; + } } } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/ListAttributeJoin.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/ListAttributeJoin.java index 56482892ff..178c0f7d9e 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/ListAttributeJoin.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/ListAttributeJoin.java @@ -134,5 +134,15 @@ public class ListAttributeJoin 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; + } } } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/MapAttributeJoin.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/MapAttributeJoin.java index d6c16ef2a5..4d7312b47e 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/MapAttributeJoin.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/MapAttributeJoin.java @@ -155,5 +155,15 @@ public class MapAttributeJoin 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; + } } } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/RootImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/RootImpl.java index df5641811c..c174362e27 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/RootImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/RootImpl.java @@ -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 extends AbstractFromImpl implements Root, 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; + } } } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/SetAttributeJoin.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/SetAttributeJoin.java index 8cfeabfa4a..0fe091bd30 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/SetAttributeJoin.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/path/SetAttributeJoin.java @@ -131,5 +131,15 @@ public class SetAttributeJoin 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; + } } } diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/TreatKeywordTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/TreatKeywordTest.java index 323760503a..0e7dfcf8fc 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/TreatKeywordTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/TreatKeywordTest.java @@ -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 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(); + } + }