title: Adding a jpa record / bean with defaults
body: <p>How can I add/persist a record/bean without a field so that it gets the default value?</p>
  <p>Trying to make a minimal example, suppose I have the postgres table created as:</p>
  <pre><code>CREATE TABLE FOO(id int, ts timestamp without time zone default current_timestamp);
  </code></pre>
  <p>and I have</p>
  <pre><code>@Entity
  @Table(name = &quot;foo&quot;)
  public class FooBean  {
      @Id
      @Column(name = &quot;id&quot;, nullable = false)
      private int id;
      @Column(name = &quot;ts&quot;, nullable = false, length = 50)
      private Instant ts;
  
      public int getId() {return id;}
      public Instant getTs() {return ts;}
  
      public void setId(int id) {
          this.id = id;
      }
      public void setTs(Instant ts) {
          this.ts = ts;
      }
  </code></pre>
  <p>}</p>
  <p>And then e.g.</p>
  <pre><code>@ApplicationScoped
  public class FooService {
      @Inject
      EntityManager em;
  
      @Transactional
      public boolean addAFoo(int id){
           //left out: make sure it is not already added, else return false, then:
           FooBean fooBean = new FooBean();
           fooBean.setId(id);
           em.persist(fooBean);
           return true;
      }
  }
  </code></pre>
  <p>The problem here is that although I did not setup explicitly fooBean.ts, it is null, and so postgres does not assign the default value, but tries to set ts to null on the created record (and we get an error,cause the column is not nullable ).</p>
  <p>How can this be overcome / how can I tell JPA to allow the DB to use the default value at insertion time?</p>