Uploaded image for project: 'Hibernate ORM'
  1. HHH-9411

Regression of Criteria Query Treat for multi-tier type hierarchy

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Blocker
    • Resolution: Fixed
    • Affects Version/s: 4.3.5, 4.3.6, 4.3.8, 5.0.2, 5.0.5
    • Fix Version/s: 5.1.0
    • 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
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true
    • Sprint:

      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:

      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):

      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:

      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
      

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: