javajpaspring-bootspring-dataobject-persistence

Spring Data JPA check if a SET contains an object


I have a domain object called Device with this property

@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
private Set<Driver> authorizedDrivers = new HashSet<>();

I would like to make this query, but I got an error (antlr.NoViableAltException: unexpected AST node: ()

@Query("select dev from Device dev where dev.authorizedDrivers.contains(?1) ")
Set<Device> findDeviceByDriver(Driver driver);

Solution

  • JPQL does not have 'contains' expression. You can try this query:

    @Query("select dev from Device dev join dev.authorizedDrivers d where d = ?1")
    Set<Device> findDeviceByDriver(Driver driver);