【问题标题】:JPA Integer Discriminator ValuesJPA 整数鉴别器值
【发布时间】:2013-06-16 18:05:35
【问题描述】:

我正在尝试遵循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

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

【问题讨论】:

    标签: java postgresql jpa


    【解决方案1】:

    在你的@DiscriminatorColumn注解上,你需要指定discriminatorType

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

    【讨论】:

    • 完美运行。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多