Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring data jpa query value in a Set

I have an entity class A which has a Set of entities of class B with a ManyToMany relationship (out of scope why I need this)

class A {

    @ManyToMany(cascade = CascadeType.ALL)
    Set<B> setOfB;
}

Now, given an object of class B, how can I retrieve the object(s) of class A which has the B object in its Set??

I have tried in my class A repository with this:

interface Arepository extends JpaRepository<A, Long> {

    @Query("from A a where ?1 in a.setOfB")
    List<A> findByB(B b)
}

But it gives me a SQLGrammarException, so which is the correct syntax?

Thank you for your help.

like image 269
Johnny Avatar asked Jan 08 '23 15:01

Johnny


1 Answers

Try with @Query("SELECT a from A a where ?1 member of a.setOfB").

like image 70
Predrag Maric Avatar answered Jan 26 '23 01:01

Predrag Maric