title: how to add subquery into specification in Java
body: <p>I wish to have subquery, which provides me filtering actors by name. </p>
  
  <p>I have a rest controller's method, which returns list of actors as JSON from movie base on movieId. I try to add filters as specification, but I have no idea how to write proper query. Base on "<a href="https://stackoverflow.com/questions/31841471/spring-data-jpa-specification-for-a-manytomany-unidirectional-relationship">Spring Data JPA Specification for a ManyToMany Unidirectional Relationship</a>" I found solution for subquery, which returns me all actors to proper movie base on movieId. Now I try to write this query.</p>
  
  <p>Actor entity</p>
  
  <pre><code>@Data
  @NoArgsConstructor
  @Entity
  @Table(name = "actors")
  public class Actor implements Serializable {
  
  private static final long serialVersionUID = 6460140826650392604L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "actor_id")
  private Long actorId;
  
  @Column(name = "first_name")
  private String firstName;
  
  @ManyToMany(mappedBy = "actors")
  @ToString.Exclude
  private List&lt;Movie&gt; movie = new ArrayList&lt;&gt;();
  
  @JsonIgnore
  public List&lt;Movie&gt; getMovie() {
      return this.movie;
  }
  }
  </code></pre>
  
  <p>Movie entity</p>
  
  <pre><code>@Data
  @Entity
  @NoArgsConstructor
  @Table(name = "movies")
  
  public class Movie implements Serializable {
  
  private static final long serialVersionUID = 3683778473783051508L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "movie_id")
  private Long movieId;
  
  private String title;
  
  @ManyToMany(cascade = { CascadeType.ALL })
  @JoinTable(name = "movies_actors"
          , joinColumns = { @JoinColumn(name = "movie_id") }
          , inverseJoinColumns = { @JoinColumn(name = "actor_id") })
  
  private List&lt;Actor&gt; actors = new ArrayList&lt;&gt;();
  
  @JsonIgnore
  public List&lt;Actor&gt; getActors() {
      return this.actors;
  }
  }
  </code></pre>
  
  <p>//Rest Controller</p>
  
  <pre><code>@CrossOrigin(origins = "http://localhost:3000")
  @RestController
  @RequestScope
  @RequestMapping("/rest")
  public class ActorRestController {
  
  private ActorService actorService;
  private MovieService movieService;
  
  @Autowired
  public ActorRestController(ActorService actorService, MovieService movieService) {
      this.actorService = actorService;
      this.movieService = movieService;
  }
  .
  .
  .
  
  @GetMapping("movies/{movieId}/actors")
  public ResponseEntity&lt;Page&lt;Actor&gt;&gt; getAllActorsFromMovieByIdMovie(@PathVariable(name = "movieId") Long movieId, Pageable pageable) {
      Optional&lt;Movie&gt; movieFromDataBase = movieService.findMovieById(movieId);
      if (movieFromDataBase.isPresent()) {
          return new ResponseEntity&lt;&gt;(actorService.findAllActors(ActorSpec.query(movieId), pageable), HttpStatus.OK);
      } else {
          return new ResponseEntity&lt;&gt;(HttpStatus.NOT_FOUND);
      }
  }
  .
  .
  }
  </code></pre>
  
  <p>// Specification for actor</p>
  
  <pre><code>@NoArgsConstructor(access = AccessLevel.PRIVATE)
  public class ActorSpec {
  
  public static Specification&lt;Actor&gt; query(final Long movieId) {
      return (root, query, cb) -&gt; {
          query.distinct(true);
          Subquery&lt;Movie&gt; movieSubQuery = query.subquery(Movie.class);
          Root&lt;Movie&gt; movie = movieSubQuery.from(Movie.class);
          Expression&lt;List&lt;Actor&gt;&gt; actors = movie.get("actors");
          movieSubQuery.select(movie);
          movieSubQuery.where(cb.equal(movie.get("movieId"), movieId), cb.isMember(root, actors));
  
          return cb.exists(movieSubQuery);
      };
   }
  
  
  }
  </code></pre>
  
  <p>I would like, my code will return filtered actors by name ex.:</p>
  
  <p><a href="http://localhost:8080/rest/movies/48/actors?name=Collin" rel="nofollow noreferrer">http://localhost:8080/rest/movies/48/actors?name=Collin</a></p>
  
  <p>will return me </p>
  
  <pre><code> { "actorId": 159,
  "firstName": "Collin",
  "lastName": "Konopelski",
  "age": 21
   },
  </code></pre>
  
  <p>but in case I do not sent any request param (<a href="http://localhost:8080/rest/movies/48/actors" rel="nofollow noreferrer">http://localhost:8080/rest/movies/48/actors</a>), let program return me all actors. I don't want to create new endpoint only for @Requestparam cause, this one is used by UI created in React. </p>
  
  <p>Thanks!</p>