Ok, if I understand this correctly animal cannot be an interface but must be some class that is an Entity. I tested it with with a class that is a @MappedSuperclass but that doesn’t work either. With animal being of type Dog it does work.
Unfortunately I can’t do this kind of change in the real application. So I tried your suggestion using cast of HibernateCriteriaBuilder. This lead to
org.hibernate.type.descriptor.java.spi.JdbcTypeRecommendationException: Could not determine recommended JdbcType for 'org.hibernate.bugs.Dog'
(criteriaBuilder.cast(animal, Dog.class))
Even if it worked, there is no way to do further joins or fetches on the dog object, no? That is because it returns a JpaExpression which doesn’t allow me to do any join or fetch.
Maybe I have a fundamental misunderstanding of the type hierarchy in Hibernate. While I can chain several joins, e.g. query.from(A.class).join("b").join("c") etc, as soon as I want to fetch something, I can’t continue, e.g. query.from(A.class).fetch("b").join("c") doesn’t work. Why is that?