0

As the title says, I'm trying to use Hibernate's @Where annotation with Spring Data JPA but without success, is it actually supposed to work together ?

Anthony Richir
  • 649
  • 2
  • 8
  • 31
  • Possible duplicate https://stackoverflow.com/questions/41453231/how-to-replicate-the-hibernates-where-functionality-in-spring-jparepository or https://stackoverflow.com/questions/45952919/how-to-add-global-where-clause-for-all-find-methods-of-spring-data-jpa-with-hibe?rq=1 – Martin Čuka Aug 14 '18 at 19:33
  • Spring Data JPA does nothing that would not make where not work. Can you post an example where you think it doesn't work? – Simon Martinelli Aug 15 '18 at 06:20
  • @MartinČuka I checked your links and my question isn't a duplicate, it's a completely different problem, please see my own answer to the question. – Anthony Richir Aug 16 '18 at 07:18

2 Answers2

2

While working on the example to demonstrate my problem, I realized what was wrong.

My problem is that the @Where annotation was set on an abstract parent class that we wanted to have for all our entities to extend and be able to soft deleted everything.

Unfortunately, it seems that, when set on a parent class, the @Where is not used (maybe on purpose and I'm not aware of it).

Here is my example which demonstrate that, when used on the entity directly, it's working, and when used on a parent class, it doesn't.

Example @Where Annotation

Anthony Richir
  • 649
  • 2
  • 8
  • 31
0

I tried to use @Where on a @OneToMany relation and I noticed that it only works when the entity is managed by Hibernate.

So when I created my entity and saved it through a Spring Data repository it doesn't worked (the collection was empty and there was no query in the log). But when I fetch the entity via the repository (using findById(...) or something) and I accessed the relation with the @Where annotation it did worked.

Maybe this is what you are experiencing as wel?

Stefan
  • 147
  • 1
  • 11