From 7612f8c1836ead3dc84c41bde717d10d9e6b7678 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 9 Nov 2015 22:41:59 -0600 Subject: [PATCH] HHH-8847 - Exception thrown when trying to select a treated property (cherry picked from commit 4132a4293a3e05770ec83b464338300c5396545e) --- .../org/hibernate/jpa/criteria/path/RootImpl.java | 6 ++++-- .../jpa/test/criteria/TreatKeywordTest.java | 13 ++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) 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 e5d390e6a1..bd98839855 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 @@ -116,12 +116,14 @@ public class RootImpl extends AbstractFromImpl implements Root, Seria @Override public void prepareAlias(RenderingContext renderingContext) { - // do nothing... + // NOTE : we call `original#prepareAlias` here and during render + // since in some cases only one or the other will be called + original.prepareAlias( renderingContext ); } @Override public String render(RenderingContext renderingContext) { - original.prepareAlias(renderingContext); + original.prepareAlias( renderingContext ); return getTreatFragment(); } 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 58ca660f6f..aa4c6637b0 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 @@ -17,8 +17,8 @@ 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.testing.TestForIssue; +import org.hibernate.testing.TestForIssue; import org.junit.Assert; import org.junit.Test; @@ -200,6 +200,17 @@ public class TreatKeywordTest extends BaseEntityManagerFunctionalTestCase { em.close(); } + @Test + public void testSelectSubclassPropertyFromDowncast() { + EntityManager em = getOrCreateEntityManager(); + CriteriaBuilder builder = em.getCriteriaBuilder(); + CriteriaQuery criteria = builder.createQuery( Integer.class ); + Root root = criteria.from( Thing.class ); + Root subroot = builder.treat( root, ThingWithQuantity.class ); + criteria.select( subroot.get( "quantity" ) ); + em.createQuery( criteria ).getResultList(); + em.close(); + } }