title: Spring: How to use KeyHolder with PostgreSQL
body: <p>Recently migrated to POSTGRESQL, I am trying to obtain the uniquely generated key on creating a new entry into the db table. The table <code>screenstable</code> looks like this:</p>
  
  <pre><code>CREATE TABLE screenstable
  (
    id serial NOT NULL,
    screenshot bytea,
    CONSTRAINT screen_id PRIMARY KEY (id )
  )
  </code></pre>
  
  <p>The method that inserts data into <code>screenstable</code> is as follows:</p>
  
  <pre><code>@Autowired NamedParameterJDBCTemplate template;
  public int insertImage(ImageBean imageBean){
          String query = "insert into screenstable (screenshot) values (:image)";
          SqlParameterSource data = new BeanPropertySqlParameterSource(imageBean);
          KeyHolder keyHolder = new GeneratedKeyHolder();
          template.update(query, data, keyHolder);
          return keyHolder.getKey().intValue();
      }
  </code></pre>
  
  <p>and <code>ImageBean</code> is</p>
  
  <pre><code>import java.util.Arrays;
  
  public class ImageBean {
      private int id;
      private byte[] image;
      @Override
      public String toString() {
          return "ImageBean [id=" + id + ", image=" + Arrays.toString(image)
                  + "]";
      }
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      public byte[] getImage() {
          return image;
      }
      public void setImage(byte[] image) {
          this.image = image;
      }
  }
  </code></pre>
  
  <p>But running the code give the following exception</p>
  
  <pre><code>15:33:20,953 ERROR JsonParseExceptionMapper:15 - org.springframework.dao.InvalidDataAccessApiUsageException: The getKey method should only be used when a single key is returned.  The current key entry contains multiple keys: [{id=3, screenshot=[B@db59df}]
  org.springframework.dao.InvalidDataAccessApiUsageException: The getKey method should only be used when a single key is returned.  The current key entry contains multiple keys: [{id=3, screenshot=[B@db59df}]
          at org.springframework.jdbc.support.GeneratedKeyHolder.getKey(GeneratedKeyHolder.java:65)
          at some.project.model.FeedbackDao.insertImage(FeedbackDao.java:20)
          at some.project.rest.FeedsRest.pluginCheck(FeedsRest.java:62)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:597).....
  </code></pre>
  
  <p>The same code used to run fine in case of <code>MySQL</code> but is failing with keys when used with POSTGRES. Is the datatype <code>serial</code> somehow responsible for the code failing or may be I am using the primary key feature correctly?</p>
  
  <p>Please advice.</p>