title: JPA Query to deleteById for a Composite Key declared using IdClass
body: <h1>Question</h1>
  <p>If I have declared my (composite) primary key using <code>@IdClass</code>, how do I write my <code>@Query</code> to be able to issue a <code>DELETE</code> query using a <code>Collection&lt;MyIdClass&gt;</code> ?</p>
  <h3>Secondary question</h3>
  <p>Will the CASCADE actually trigger the deletion of the associated <code>AnotherEntity</code> despite using <code>@Query</code>?</p>
  <h1>Current model</h1>
  <pre class="lang-java prettyprint-override"><code>@Entity
  @Table(name = &quot;myentity&quot;)
  @JsonIgnoreProperties(ignoreUnknown = true)
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  @Builder
  @IdClass(MyIdClass.class)
  public class MyEntity {
  
      @Id
      @Column(updatable = false)
      private String foo;
  
      @Id
      @Column(updatable = false)
      private String bar;
  
      @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
      @JoinColumn(name = &quot;my_foreign_key&quot;, referencedColumnName = &quot;external_pk&quot;)
      private AnotherEntity anotherEntity;
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>@Data
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  public class MyIdClass implements Serializable {
  
      private String foo;
      private String bar;
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>@Entity
  @Table(name = &quot;anotherentity&quot;)
  @JsonIgnoreProperties(ignoreUnknown = true)
  @Data
  @SuperBuilder
  @NoArgsConstructor
  @AllArgsConstructor
  public class AnotherEntity {
  
      @Id
      @Column(name = &quot;external_pk&quot;, nullable = false, updatable = false)
      private String externalPk;
  }
  </code></pre>
  <h1>What I've read</h1>
  <p>A few resources:</p>
  <ol>
  <li><a href="https://www.baeldung.com/spring-data-jpa-query" rel="nofollow noreferrer">https://www.baeldung.com/spring-data-jpa-query</a></li>
  <li><a href="https://www.baeldung.com/spring-data-jpa-delete" rel="nofollow noreferrer">https://www.baeldung.com/spring-data-jpa-delete</a></li>
  <li><a href="https://stackoverflow.com/a/36765129/9768291">https://stackoverflow.com/a/36765129/9768291</a></li>
  </ol>
  <p>And I also found <a href="https://stackoverflow.com/questions/55877193/spring-jpa-repository-jpql-query-using-a-collection-of-objects">this SO question</a> which seemed very close to what I'm looking for, but unfortunately there are no answers.</p>
  <h1>Goal</h1>
  <p>Something similar to:</p>
  <pre class="lang-java prettyprint-override"><code>@Repository
  public interface MyCRUDRepository extends CrudRepository&lt;MyEntity, MyIdClass&gt; {
  
      @Modifying
      @Query(&quot;DELETE FROM myentity m WHERE m IN ?1&quot;) // how do I write this?
      void deleteAllWithIds(Collection&lt;MyIdClass&gt; ids);
  }
  </code></pre>
  <p>Ultimately, I want to do this to batch my DELETE requests to increase the performance.</p>
  <h2>Pitfalls I'm trying to avoid</h2>
  <p>I know there is a <code>deleteAll(Iterable&lt;? extends MyEntity&gt;)</code> but then I need to actually have those entities to begin with, which would require extra calls to the DB.</p>
  <p>There is also <code>deleteById(MyIdClass)</code>, but that actually always issues a <code>findById</code> before sending a single DELETE statement as a transaction: not good for the performance!</p>
  <h2>Potentially irrelevant precision</h2>
  <p>I'm not sure if that can help, but my JPA provider is <code>EclipseLink</code>. My understanding is that there are properties for batching requests, and that's ultimately what I'm aiming to use.</p>
  <p>However, I'm not entirely sure what are the internal requirements for that batching to happen. For example, if I did a <code>deleteById</code> in a <code>for-loop</code>, would the alternating <code>SELECT</code> and <code>DELETE</code> statements prevent the batching from happening? The documentation is quite scarce about that.</p>