我正在尝试遵循JPA 教程并设计一个类层次结构来表示下表:
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); }
}
但是,这不起作用,因为 theentity_type
是一个INT
而不是String
:
内部异常:org.postgresql.util.PSQLException:错误:列“entity_type”是整数类型,但表达式的类型是字符变化
但是如果我更改@DiscriminatorValue("1")
为@DiscriminatorValue(1)
,我会得到一个编译错误:
类型不匹配:无法从 int 转换为 String
我这里需要一个整数。有什么快速的建议吗?