title: Is it possible to store null value of an enumerated attribute to database?
body: <p>I am trying to store <strong>null</strong> value of an <strong>enumerated</strong> attribute of an entity in <strong>Java</strong> to <strong>Oracle</strong> database using JPA TopLink. The code is executed with no exception or warning, but the entity is not stored in the database.</p>
  
  <p>The entity is defined as follows:</p>
  
  <pre><code>@Entity
  public class LetterDoc {
      ...
      @Column(name = "delivery_type", columnDefinition = "VARCHAR2(20)", nullable = true)
      @Enumerated(EnumType.STRING)
      private DocDeliveryTypeEnum deliveryType;
      ...
  }
  </code></pre>
  
  <p>The Enum class DocDeliveryTypeEnum have only required values.</p>
  
  <pre><code>public enum DocDeliveryTypeEnum {
      NORMAL,
      RECOMMENDED,
      ACKNOWLEDGEMENT
  }  
  </code></pre>
  
  <p>In my situation, I have instances of entities where the deliveryType makes sence and is required. However, for some cases, the deliveryType is <strong>irrelevant</strong> and I want to leave it <strong>null</strong>. I don't want to set a default value as there is no adequate value for this case.</p>
  
  <p>The entity instance has the value set to null, but when I try to persist and flush the instance in the database, it is just not stored, although the column is defined as VARCHAR2(20) with NULL value enabled.</p>
  
  <p>The situation is something like this:</p>
  
  <pre><code>LetterDoc let = new LetterDoc();
  // settery ostatnich atributu
  let.setDeliveryType(null);
  try {
      emptyDocDAO.persist(let);
      emptyDocDAO.flush();
  } catch (Exception e) {
      // no exception is really catched :(
      log.error(e);
  }
  // here I have the id of the entity
  </code></pre>
  
  <p>Really no exception is thrown while persisting, just the entity is not stored. When I try to get the entity later in another thread by the ID, it fails with NoResultException.</p>
  
  <p>Is there any chance to make this work, or do I really have to set a value for all enumerated attributes before persisting? Do I really have to create a default value in the Enum class?</p>
  
  <p>I have solved my problem by setting a default value that does not make much sense in the particular situation. However, I wonder if there is any other option.</p>
  
  <p>Thank you very much for your experience and advice.</p>