title: Avoid insert 'null' values to database table via JPA
body: <p>im starting with JPA2 and feel quite comfortbale so far. But I have a problem when persisting Entities with null property values for NON NULL database fields with default value.</p>
  
  <p>I would like to be able to leave the entity property null and let the database insert the default value.</p>
  
  <p>My current setup is openJPA with PostgreSQL.</p>
  
  <p>I have this VERSION database table (Vorgabewert = Default value):</p>
  
  <pre>
  <code>
       Spalte     |             Typ             |         Attribute
  ----------------+-----------------------------+----------------------------
   status_        | smallint                    | not null Vorgabewert 0
   time_          | timestamp without time zone | not null
   system_time    | timestamp without time zone | not null Vorgabewert now()
   version        | character varying(20)       | not null
   activationtime | timestamp without time zone |
   importtime     | timestamp without time zone |
  </code>
  </pre>
  
  <p>I have an entity (Java DTO) which maps the database fields (except 'status') by xml configuration.</p>
  
  <p>I hoped I could insert an entity without the <code>system_time</code> set and expected that the database will fill the current time as default value.</p>
  
  <p>JPA constructs the following SQL-Query:</p>
  
  <pre><code>INSERT INTO public.version (version, activationtime, importtime, system_time, time_) VALUES (?, ?, ?, ?, ?) [params=?, ?, ?, ?, ?]
  </code></pre>
  
  <p>and Postgres reacts with:</p>
  
  <p><code>FEHLER:  NULL-Wert in Spalte »system_time« verletzt Not-Null-Constraint</code> (sorry for German language but this message means the Not-Null-Constraint violation on 'system_time').</p>
  
  <p>So what can I do? Is this a JPA or Database Problem.
  Can I configure JPA to exclude null properties from the INSERT SQL Statement.</p>
  
  <p>I want to have the ability to set the 'system_time' in my entity or to let it be 'null' and let the database put the default value.</p>
  
  <p>Any help is welcome!</p>
  
  <p>Regads
    Klaus</p>