title: Hibernate 4: persisting InheritanceType.JOINED discriminator column values
body: <p>I have a simple JOINED hierarchy of documents:</p>
  
  <pre><code>CREATE TABLE Documents
  (
    id INTEGER NOT NULL,
    discriminator ENUM('official','individual','external') NOT NULL,
    file_name VARCHAR(200) NOT NULL,
    PRIMARY KEY (id)
  );
  
  CREATE SystemDocuments
  (
    id INTEGER NOT NULL,
    binary_data BLOB NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (id) REFERENCES Documents (id)
  );
  
  CREATE ExternalDocuments
  (
    id INTEGER NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (id) REFERENCES SystemDocuments (id)
  );
  </code></pre>
  
  <p>As you can see all the sub tables do is share the same ID from the Documents table. Other than that <code>SystemDocuments</code> adds a <code>binary_data</code> column and <code>ExternalDocuments</code> adds no new properties. (Also note there are two other concrete sub tables in the hierarchy denoted by <code>'official'</code> and <code>'individual'</code> which are of no relevance here.)</p>
  
  <p>Here are the mappings for the above tables:</p>
  
  <p><strong>Document.java</strong>:</p>
  
  <pre><code>@Entity
  @Table(name = "Documents")
  @Inheritance(strategy = InheritanceType.JOINED)
  @DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
  //@DiscriminatorOptions(force = true) // &lt;-- Hibernate 4-specific annotation not inserting discriminator values
  public abstract class Document implements Serializable
  {
      @Id
      @Column
      protected Integer id;
  
      @Column(name = "file_name")
      protected String fileName;
  
      ...
  }
  </code></pre>
  
  <p><strong>SystemDocument.java</strong>:</p>
  
  <pre><code>@Entity
  @Table(name = "SystemDocuments")
  public abstract class SystemDocument extends Document
  {
      @Lob
      @Column(name = "binary_data")
      protected byte[] binaryData;
  
      ...
  }
  </code></pre>
  
  <p><strong>ExternalDocument.java</strong>:</p>
  
  <pre><code>@Entity
  @Table(name = "ExternalDocuments")
  @DiscriminatorValue(value = "external")
  public class ExternalDocument extends SystemDocument
  {
      ...
  }
  </code></pre>
  
  <p>The latter class is supposed to be mapped to the Documents' discriminator column value <code>'external'</code>. When finding entities via EntityManager.find the discriminators are returned correctly, well actually because the discriminators of my test data have been INSERTed into the DB correctly.</p>
  
  <p>Now I use the following code to insert new documents/files into the system via JPA and a file uploader:</p>
  
  <pre><code>...
  
  UploadedFile uf = event.getUploadedFile();
  
  // set ID, file name, and binary data
  ExternalDocument detachedExternalDocument =
      new ExternalDocument(1234567, uf.getName(), uf.getData());
  
  docService.create(detachedExternalDocument);
  </code></pre>
  
  <p>When inspecting the DB however I can see that Hibernate does <strong>not</strong> insert the <code>'external'</code> discriminator value into the <code>Documents</code> table's <code>discriminator</code> column.</p>
  
  <p>There have been issues about this in the past, see <a href="https://hibernate.onjira.com/browse/ANN-140" rel="noreferrer">https://hibernate.onjira.com/browse/ANN-140</a> and more recently for Hibernate 4 <a href="https://hibernate.onjira.com/browse/HHH-4358" rel="noreferrer">https://hibernate.onjira.com/browse/HHH-4358</a>, so chances are it's supposed to work that way.</p>
  
  <p>I then found <a href="http://docs.jboss.org/hibernate/core/4.0/javadocs/org/hibernate/annotations/DiscriminatorOptions.html" rel="noreferrer">http://docs.jboss.org/hibernate/core/4.0/javadocs/org/hibernate/annotations/DiscriminatorOptions.html</a> in the current Hibernate 4 API Docs, but it doesn't work (see @DiscriminatorOptions at the Document class).</p>
  
  <p>How can I get Hibernate 4 to insert the discriminators <strong>using raw annotations</strong>?</p>
  
  <p><strong>Note</strong>: I don't want to map the discriminator column as a regular column.</p>