|
I use joined inheritance mapping for my tables, and found to my dismay that using the JPA 2.1 "treat" construct to downcast causes the usual left outer join to a subclass table to become an inner join. This makes the construct useless since the inner join makes polymorphic queries impossible: only the treat-ed type will be retrieved. In the case where 2 or more downcasts are made, absolutely no records can be retrieved.
Sample JPA 2.1 code:
CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> query = cb.createQuery(Object[].class); Root<Pet> root = query.from(Pet.class); query.multiselect( root.get("id"), root.get("name"), cb.treat(root, Cat.class).get("felineProperty"), cb.treat(root, Dog.class).get("canineProperty") );
The resulting SQL looks like (mocked up):
select ... from Pet pet0_ left outer join Bunny bunny0_ on pet0_.ID=bunny0_.ID inner join Cat cat0_ on pet0_.ID=cat0_.ID inner join Dog dog0_ on pet0_.ID=dog0_.ID
|