0

I have the following hibernate query

findByUserOrReaderAndIdInAndStatusNotIn

the actual query generated is

WHERE user=? OR reader=? AND (id in (?, ?)) AND (status not in (?, ?))

but what I actually need is

WHERE (user=? OR reader=?) AND (id in (?, ?)) AND (status not in (?, ?))

How to properly create the findBy method? I cannot use @Query cause this method is generic.

coladict
  • 4,799
  • 1
  • 16
  • 27
iPhoneJavaDev
  • 821
  • 5
  • 33
  • 78
  • My guess is that you'll have to use an explicit query with the `@Query` annotation. The JPA provided methods are for convenience only, there are usually instances where they won't do exactly what you need. – Tim Biegeleisen Aug 18 '21 at 03:24
  • "@Query" can be used for generic purpose also. @Query gives you more control than the method name derived queries. – Lucia Aug 18 '21 at 07:10
  • Does this answer your question? [Spring data jpa - How to combine multiple And and Or through method name](https://stackoverflow.com/questions/35788856/spring-data-jpa-how-to-combine-multiple-and-and-or-through-method-name) – İsmail Y. Aug 18 '21 at 07:42

1 Answers1

0

My problem is the generics, so I did the following and it works.

@Query("from #{#entityName} t where (t.user = :user or t.reader = :reader) and (t.id in (:ids)) and (t.status not in (:statuses))")
iPhoneJavaDev
  • 821
  • 5
  • 33
  • 78