title: How to get new record ID after native insert
body: <p>For some good reasons i do a spring data jpa native insert.<br>
  The query gets executed in the right way.<br>
  But what i need is the newly generated table id getting generated by the <code>nextval('hibernate_sequence')</code></p>
  
  <p>Is there a way to get this id?</p>
  
  <p>Here is my query:</p>
  
  <pre><code>/**
   * Inserts a new file attachment.
   * This is useful and maybe better suitable, because one may not want to load the whole
   * job offer to fullfill the JobOffer -&gt; FileAttachment OneToMany Relation
   *
   * @param file       a given file
   * @param fileName   a given file name
   * @param jobOfferId a given job offer id
   * @return int the new id
   */
  @Modifying
  @Query(value = "insert into fileattachment(fileattachmentid, file, filename, joboffer_jobofferid) values (nextval('hibernate_sequence'), ?1, ?2, ?3);", nativeQuery = true)
  int insertFileAttachment(String file, String fileName, long jobOfferId);
  </code></pre>
  
  <p>The int return value just gives the number of inserted records (1).<br>
  But i need the newly Id.<br>
  I don´t want to query it after the insert by another database query. Because if i have to, the whole native insert gets obsolete.</p>
  
  <p>Does someone know an answer/Has someone alternative tips?<br>
  Thank you!<br>
  Kind regards<br>
  Thomas</p>
  
  <p><strong>Edit</strong>:<br>
  I use the native insert to avoid loading the whole joboffer record, which is a lot of useless data, just to persist the data the way with the entitymanager.</p>
  
  <p>Instead i insert the data native.</p>
  
  <p>Anyway your tip with the returning data from insert statements was very cool.<br>
  I was giving that a try and it is working. Thank you very very much!<br>
  I ended up with this solution:  </p>
  
  <pre><code>/**
   * Inserts a new file attachment.
   * This is useful and maybe better suitable, because one may not want to load the whole
   * job offer to fullfill the JobOffer -&gt; FileAttachment OneToMany Relation
   *
   * @param file       a given file
   * @param fileName   a given file name
   * @param jobOfferId a given job offer id
   * @return int the new id
   */
  @Query(value = "insert into fileattachment(fileattachmentid, file, filename, joboffer_jobofferid) values (nextval('hibernate_sequence'), ?1, ?2, ?3) returning fileattachmentid;", nativeQuery = true)
  long insertFileAttachment(String file, String fileName, long jobOfferId);
  </code></pre>