title: jooq - exclude id field when inserting from POJO
body: <p>Is there anyway to insert a new record into a PostgreSQL database with Jooq straight from a POJO which extends a general identity class that has an id field without including the id in the insert statement?</p>
  
  <p>An example POJO:</p>
  
  <pre><code>@Data
  public abstract class PersistenceIdentity {
      @Id
      @Column(name = "id", unique = true, nullable = false, precision = 7, insertable = false)
      private Integer id;
  
      @Column(name = "created_date")
      private LocalDateTime createdDate;
  
      public abstract Table&lt;R&gt; getJooqTable();
  }
  
  @Data
  public class SocialNetwork extends PersistenceIdentity {
  
      @Column(name = "name")
      private String name;
  
      @Override
      public Table&lt;SocialNetworkRecord&gt; getJooqTable() {
          return Tables.SOCIAL_NETWORK;
      }
  
  }
  </code></pre>
  
  <p>The PostgreSQL schema is:</p>
  
  <pre><code>CREATE TABLE "social_network" (
      id              SERIAL NOT NULL PRIMARY KEY,
      created_date    TIMESTAMP DEFAULT now(),
      name            TEXT NOT NULL
  );
  </code></pre>
  
  <p>My code to persist the POJO:</p>
  
  <pre><code>public &lt;T extends PersistenceIdentity&gt; T insertRecord(T record) {
      Record newRecord = db.newRecord(record.getJooqTable(), record);
      if (newRecord instanceof UpdatableRecord) {
          ((UpdatableRecord) newRecord).store();
      }
  
      return newRecord.into(record);
  }
  </code></pre>
  
  <p>I realize I'm probably doing what Jooq really wasn't meant for (i.e. using generic types), however that (appears) to work just fine.</p>
  
  <p>The problem is, Jooq includes the id in the insert statement and I then, of course, get a null value constraint.  I don't want it inserted when it's a new record, however I do want it included when it returns the record (after inserting), when updating and also in select statements.</p>
  
  <p>I can't simply exclude the id because I need it later on to easily get around some of the @OneToMany / @ManyToOne limitations.</p>
  
  <p>And I would rather not have to insert the specific values for each POJO (that's why we annotated with @Column).</p>
  
  <p>Does Jooq not honor the @Id or the insertable = false parameter in @Column?</p>
  
  <p>Can anyone shed some light on this?</p>
  
  <p><strong>EDIT 1</strong>
  Per request, below is the relevant snippet from the jOOQ generated table object.  I'm not sure if this is correct or not for what I'm trying to do (i.e. allow the database to generate the ID), but I would think <code>nextval('social_network_id_seq'::regclass)</code> would accomplish that.</p>
  
  <pre><code>@Generated(
      value = {
          "http://www.jooq.org",
          "jOOQ version:3.9.1"
      },
      comments = "This class is generated by jOOQ"
  )
  @SuppressWarnings({ "all", "unchecked", "rawtypes" })
  public class SocialNetwork extends TableImpl&lt;SocialNetworkRecord&gt; {
  
      /**
       * The column &lt;code&gt;public.social_network.id&lt;/code&gt;.
       */
      public final TableField&lt;SocialNetworkRecord, Integer&gt; ID = createField("id", org.jooq.impl.SQLDataType.INTEGER.defaultValue(org.jooq.impl.DSL.field("nextval('social_network_id_seq'::regclass)", org.jooq.impl.SQLDataType.INTEGER)), this, "");
  
  }
  </code></pre>
  
  <p>Also, we use the <code>mvn jooq-codegen:generate -Djooq.generator.name=org.jooq.util.XMLGenerator</code> to generate the XML schema and then generate the the jOOQ table objects from that XML config.  The thinking is we can push the XML config to github and all builds can simply regenerate the table objects from that.  </p>
  
  <p>Here is the XML:</p>
  
  <pre><code>&lt;column&gt;
      &lt;table_catalog&gt;&lt;/table_catalog&gt;
      &lt;table_schema&gt;public&lt;/table_schema&gt;
      &lt;table_name&gt;social_network&lt;/table_name&gt;
      &lt;column_name&gt;id&lt;/column_name&gt;
      &lt;data_type&gt;integer&lt;/data_type&gt;
      &lt;character_maximum_length&gt;0&lt;/character_maximum_length&gt;
      &lt;numeric_precision&gt;32&lt;/numeric_precision&gt;
      &lt;numeric_scale&gt;0&lt;/numeric_scale&gt;
      &lt;ordinal_position&gt;1&lt;/ordinal_position&gt;
      &lt;column_default&gt;nextval('social_network_id_seq'::regclass)&lt;/column_default&gt;
  &lt;/column&gt;
  &lt;table_constraint&gt;
      &lt;constraint_catalog&gt;&lt;/constraint_catalog&gt;
      &lt;constraint_schema&gt;public&lt;/constraint_schema&gt;
      &lt;constraint_name&gt;social_network_pkey&lt;/constraint_name&gt;
      &lt;constraint_type&gt;PRIMARY KEY&lt;/constraint_type&gt;
      &lt;table_catalog&gt;&lt;/table_catalog&gt;
      &lt;table_schema&gt;public&lt;/table_schema&gt;
      &lt;table_name&gt;social_network&lt;/table_name&gt;
  &lt;/table_constraint&gt;
  &lt;table_constraint&gt;
      &lt;constraint_catalog&gt;&lt;/constraint_catalog&gt;
      &lt;constraint_schema&gt;public&lt;/constraint_schema&gt;
      &lt;constraint_name&gt;2200_17431_1_not_null&lt;/constraint_name&gt;
      &lt;constraint_type&gt;CHECK&lt;/constraint_type&gt;
      &lt;table_catalog&gt;&lt;/table_catalog&gt;
      &lt;table_schema&gt;public&lt;/table_schema&gt;
      &lt;table_name&gt;social_network&lt;/table_name&gt;
  &lt;/table_constraint&gt;
  &lt;table_constraint&gt;
      &lt;constraint_catalog&gt;&lt;/constraint_catalog&gt;
      &lt;constraint_schema&gt;public&lt;/constraint_schema&gt;
      &lt;constraint_name&gt;2200_17431_3_not_null&lt;/constraint_name&gt;
      &lt;constraint_type&gt;CHECK&lt;/constraint_type&gt;
      &lt;table_catalog&gt;&lt;/table_catalog&gt;
      &lt;table_schema&gt;public&lt;/table_schema&gt;
      &lt;table_name&gt;social_network&lt;/table_name&gt;
  &lt;/table_constraint&gt;
  &lt;key_column_usage&gt;
      &lt;column_name&gt;id&lt;/column_name&gt;
      &lt;constraint_catalog&gt;&lt;/constraint_catalog&gt;
      &lt;constraint_schema&gt;public&lt;/constraint_schema&gt;
      &lt;constraint_name&gt;social_network_pkey&lt;/constraint_name&gt;
      &lt;ordinal_position&gt;0&lt;/ordinal_position&gt;
      &lt;table_catalog&gt;&lt;/table_catalog&gt;
      &lt;table_schema&gt;public&lt;/table_schema&gt;
      &lt;table_name&gt;social_network&lt;/table_name&gt;
  &lt;/key_column_usage&gt;
  </code></pre>
  
  <p><strong>EDIT 2</strong> 
  My SocialNetwork jOOQ-generated table object does not have a <code>getIdentity()</code> method, however it does have a <code>getPrimaryKey()</code> method and if it helps, my SocialNetworkRecord class has two Constructors:</p>
  
  <pre><code>public SocialNetworkRecord() {
      super(SocialNetwork.SOCIAL_NETWORK);
  }
  
  /**
   * Create a detached, initialised SocialNetworkRecord
   */
  public SocialNetworkRecord(Integer id, Timestamp createdDate, String name) {
      super(SocialNetwork.SOCIAL_NETWORK);
  
      set(0, id);
      set(1, createdDate);
      set(2, name);
  }
  </code></pre>