title: JPA Integer Discriminator Values
body: <p>I'm trying to follow the <a href="http://en.wikibooks.org/wiki/Java_Persistence/Inheritance" rel="nofollow">JPA tutorial</a> and design a class hierarchy to represent the following table:</p>
  
  <pre><code>CREATE TABLE Entities (
    entity_id INT PRIMARY KEY,
    entity_type INT CHECK (0 &lt;= entity_type AND entity_type &lt;= 2)
    entity_name VARCHAR(255)
  );
  </code></pre>
  
  <p>This table is mapped into a class hierarchy:</p>
  
  <pre><code>@Entity
  @Inheritance
  @Table(schema="myschema", name="Entities")
  @DiscriminatorColumn(name="entity_type")
  @SequenceGenerator(schema="myschema", name="entity_id_seq", sequenceName="entity_id_seq", allocationSize=100)
  public abstract class LOTEntity {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="entity_id_seq")
    @Column(name="entity_id")
    protected long entityId;
  
    @Column(name="entity_name")
    protected String entityName = "";
  
    public LOTEntity() {}
    public LOTEntity(String name) { this.entityName = name; }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Entity
  @DiscriminatorValue("1")
  class LOTClass extends LOTEntity {
    public LOTClass() {}
    public LOTClass(String name) { super(name); }
  }
  </code></pre>
  
  <p>This does not work, however, since the <code>entity_type</code> is an <code>INT</code> rather than <code>String</code>:</p>
  
  <blockquote>
    <p>Internal Exception: org.postgresql.util.PSQLException: ERROR: column
    "entity_type" is of type integer but expression is of type character
    varying</p>
  </blockquote>
  
  <p>But if I change <code>@DiscriminatorValue("1")</code> to <code>@DiscriminatorValue(1)</code>, I get a compile error:</p>
  
  <blockquote>
    <p>Type mismatch: cannot convert from int to String</p>
  </blockquote>
  
  <p>I need an integer here. Any quick suggestions?</p>