Shorn
Shorn

Reputation: 21496

How to opt out of a @Where filter for an individual JPQL query?

I have an entity mapped with a @Where annotation like:

@Where(clause = "is_active = 1")

That works fine, but now I want to write a query that doesn't use the annotated filter condition in the where clause.

The only way I can think of is to fall back to a native query.

Is there a way to disable a filter for a single specific query?

I'm using a JpaRepository @Query annotated interface method to declare my query like:

 @Query(name = "UnmergeStudent", value = "select .... student_1.lui = :lui")
 List<UnmergeStudentSummary> findMergedStudentSummaryByLui(@Param("lui") String lui);

I looked at the members of the @Query annotation but can't see anything obvious. Is there a way to avoid filters if I fall back to writing the actual query code in a normal method?

Hibernate version is 5.2.10.Final

Upvotes: 1

Views: 1294

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36203

You cannot disable @Where but you could use @Filter instead.

The difference between @Where and @Filter is that @Filter can be parameterized and @Filter has a name and must be turned on using that name:

entityManager
    .unwrap(Session.class)
    .enableFilter("your_filter");

Read more about this in the offical documentation:

https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#pc-filtering

Upvotes: 1

Related Questions