JPA整数判别器值

时间:2013-06-20 02:08:34

标签: java postgresql jpa

我正在尝试关注JPA tutorial并设计一个类层次结构来表示下表:

CREATE TABLE Entities (
  entity_id INT PRIMARY KEY,
  entity_type INT CHECK (0 <= entity_type AND entity_type <= 2)
  entity_name VARCHAR(255)
);

此表映射到类层次结构:

@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; }
}

@Entity
@DiscriminatorValue("1")
class LOTClass extends LOTEntity {
  public LOTClass() {}
  public LOTClass(String name) { super(name); }
}

但这不起作用,因为entity_typeINT而不是String

  

内部异常:org.postgresql.util.PSQLException:错误:列   “entity_type”的类型为整数,但表达式的类型为字符   变化

但如果我将@DiscriminatorValue("1")更改为@DiscriminatorValue(1),我会收到编译错误:

  

类型不匹配:无法从int转换为String

我需要一个整数。有什么快速建议吗?

1 个答案:

答案 0 :(得分:9)

@DiscriminatorColumn注释中,您需要指定discriminatorType

@DiscriminatorColumn(name="entity_type", discriminatorType=DiscriminatorType.INTEGER)