title: Insert rows into a join table directly in JPA / JPQL?
body: <p>Is there a way in JPA to insert rows directly into a join table?  I have the following Contract and Attachment entities:</p>
  
  <pre><code>public class Contract implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private Long id;
  
      @Version
      @Column(name = "version")
      private Integer version;
  
      private String number;
      private String volume;
  
      @ManyToMany
      @JoinTable(joinColumns = @JoinColumn(name = "contract_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "attachment_id", referencedColumnName = "id"))
      private List&lt;Attachment&gt; attachments;
  }
  </code></pre>
  
  <p><code>Attachment</code> entity:</p>
  
  <pre><code>public class Attachment implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private Long id;
  
      @Version
      @Column(name = "version")
      private Integer version;
  
      private String name;
  }
  </code></pre>
  
  <p>Due to thread concurrency issues, I would like to add attachments to the contract directly without needing to load the contract, add the Attachment to the list and persist/save the Contract, since there is not data being updated in the Contract object itself.  To be a little more verbose, I have multiple threads in parallel saving attachments, and if I were to persist the Contract in all those threads, I would need to put in some kind of concurrency control to avoid OptimisticLockExceptions.  Given that the Contract entity is not being modified (only the join table is), I do not want to put in a bottleneck and only update the join table directly.</p>
  
  <p>Is there a way in JPA and/or JPQL to insert rows directly into the join table?  Workflow would be to persist the Attachment, and then insert the appropriate row into the join table.</p>
  
  <p>Is there a way to retrieve the JPA-generated join-table name from the entity?</p>
  
  <p>DDL:</p>
  
  <pre><code>CREATE TABLE `contract` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `number` varchar(255) DEFAULT NULL,
    `volume` varchar(255) DEFAULT NULL,
    `version` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`),
  ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
  
  
  CREATE TABLE `attachment` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) DEFAULT NULL,
    `version` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`),
  ) ENGINE=InnoDB AUTO_INCREMENT=197 DEFAULT CHARSET=utf8;
  
  
  CREATE TABLE `contract_attachment` (
    `contract_id` bigint(20) NOT NULL,
    `attachment_id` bigint(20) NOT NULL,
    UNIQUE KEY `UK_27094bpt88d7bfngq5v52jrs` (`attachment_id`),
    KEY `FK_i1ycj6ewd536bky08t0jjh15d` (`contract_id`),
    CONSTRAINT `FK_i1ycj6ewd536bky08t0jjh15d` FOREIGN KEY (`contract_id`) REFERENCES `contract` (`id`),
    CONSTRAINT `FK_27094bpt88d7bfngq5v52jrs` FOREIGN KEY (`attachment_id`) REFERENCES `attachment` (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  </code></pre>