Hibernate 5.2 added support of JPA unassociated outer join feature, i.e., queries with outer joins between entities that don't have references to each other. However, Hibernate Filters defined through @FilterDef/@Filter don't get properly applied to entities brought in by the outer joins.
In the attached example, we have a BookEntity and a MovieEntity, both have a "name" column and an "active" column. An activeFilter is defined to limit queries to return only rows that are active: {code} @FilterDef(name= BookEntity.activeFilter, defaultCondition = "active=true") @Filter(name= BookEntity.activeFilter) {code} It's applied to both BookEntity and MovieEntity.
Now we have a query with outer join from BookEntity to MovieEntity: {code} @NamedQuery(name= BookEntity.matchBookMovieNames, query="select b, m from BookEntity b left outer join MovieEntity m on b.name=m.name") . . . {code} {code} em.unwrap(Session.class) .enableFilter(BookEntity.activeFilter); return em.createNamedQuery(BookEntity.matchBookMovieNames) .getResultList(); {code} Although the query result contains only active BookEntities, it includes inactive MovieEntities in addition to active ones, which indicates that the filter is not applied to the latter, as opposed to the inner join situation, where the filter is applied to both. Outer joins though associated entities do work properly.
Here is the SQL that Hibernate generates: {code} select bookentity0_.id as id1_0_0_, movieentit1_.id as id1_1_1_, bookentity0_.active as active2_0_0_, bookentity0_.name as name3_0_0_, movieentit1_.active as active2_1_1_, movieentit1_.name as name3_1_1_ from book_entity bookentity0_ left outer join movie_entity movieentit1_ on (bookentity0_.name=movieentit1_.name) where bookentity0_.active=1 {code} Note that movieentit1_.active=1 is missing from the query. |
|