We're updating the issue view to help you get more done. 

Regression of Criteria Query Treat for multi-tier type hierarchy

Description

In the two listed affects versions, a regression has been discovered which was not present in versions in the 4.3.x stream previous to these. The problem affects an entity domain with a three tier type hierarchy where the middle tier has a treated property and we are expecting to get back all of the root super-type in the from. Here is the test case to illustrate this:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 public class Main { @Entity public static abstract class Animal { @Id @GeneratedValue private Long id; } @Entity public static abstract class Dog extends Animal { private boolean fast; protected Dog(boolean fast) { this.fast = fast; } public final boolean isFast() { return fast; } } @Entity public static class Dachshund extends Dog { public Dachshund() { super(false); } } @Entity public static class Greyhound extends Dog { public Greyhound() { super(true); } } @Test public void treatAsSuperclassUsingCriteriaApi() { EntityManager entityManager = getEntityManager(); EntityTransaction entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); Greyhound greyhound = new Greyhound(); Dachshund dachshund = new Dachshund(); entityManager.persist(greyhound); entityManager.persist(dachshund); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Animal> criteriaQuery = cb.createQuery(Animal.class); Root<Animal> animal = criteriaQuery.from(Animal.class); Root<Dog> dog = cb.treat(animal, Dog.class); criteriaQuery.where(cb.isTrue(dog.<Boolean>get("fast"))); List<Animal> results = entityManager.createQuery( criteriaQuery).getResultList(); assertEquals(asList(greyhound), results); entityTransaction.commit(); } }

This results in no records found due to the query generated containing an odd addition restriction (1=2):

1 select main_anima0_.id as id2_0_, main_anima0_.fast as fast3_0_, main_anima0_.DTYPE as DTYPE1_0_ from Main$Animal main_anima0_ where 1=2 and main_anima0_.fast=1

On previous versions (e.g. 4.3.4), this same code generates the following correct query:

1 select main_anima0_.id as id2_0_, main_anima0_.fast as fast3_0_, main_anima0_.DTYPE as DTYPE1_0_ from Main$Animal main_anima0_ where main_anima0_.fast=1

Environment

Java version "1.7.0_65", "1.8.0_65" Java(TM) SE Runtime Environment (build 1.7.0_65-b17) Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode) OSX Yosemite

Status

Assignee

Unassigned

Reporter

Matt Todd

Labels

Components

Fix versions

Affects versions

4.3.5
4.3.6
4.3.8
5.0.2
5.0.5

Priority

Blocker