Override @Where condition clause in Hibernate 4.3.4

I have a sample code like this -

@Entity @Table(name = "employee") @Where(clause = "active IS TRUE") public class Employee{ } 

This will retrieve all records of the employee table with active = true or 1. In some cases, it may be necessary for me to want to load records with active = false or 0.

If I write my code as FROM Employee emp WHERE emp.active IS FALSE , but the generated query contains bot conditions specified in HQL and Annotations.

Therefore, the expected results do not come. Is there a way to override this predefined @Where specific object?

+6
source share
2 answers

AFAIK you cannot override it inside your class, because if you look at @ Where the documentation you will see that this interface is annotated @Retention(value=RUNTIME) , therefore it has RUNTIME as RetentionPolicy , and you can see in the documentation RetentionPolicy , which :

RUNTIME: Annotations must be written to the class file by the compiler and saved by the VM at run time, so they can be read with reflexivity.

What is the power of @Where annotation to be written to the class file by the compiler and saved by the VM at runtime, so it will be applied throughout the class.

+1
source

I know this is too old a question, but I ran into the same problem and thought I had to share my solution.

I completely agree with @ cnลdk's answer, since you cannot override, but you can ignore the @Where by defining nativeQuery as nativeQuery below:

@Query(value = "Select * from customer where company_id =?1", nativeQuery = true) List<Customer> findByCompanyIdIgnoringEntityWhere(Long companyId);

SQL in the @Query annotation must specify the table name and field names (not the name of the object).

0
source

All Articles