Yan*_*ang 3 java postgresql jpa
我正在尝试遵循JPA教程并设计一个类层次结构来表示下表:
CREATE TABLE Entities (
entity_id INT PRIMARY KEY,
entity_type INT CHECK (0 <= entity_type AND entity_type <= 2)
entity_name VARCHAR(255)
);
Run Code Online (Sandbox Code Playgroud)
此表映射到类层次结构:
@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; }
}
Run Code Online (Sandbox Code Playgroud)
和
@Entity
@DiscriminatorValue("1")
class LOTClass extends LOTEntity {
public LOTClass() {}
public LOTClass(String name) { super(name); }
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,但是,因为entity_type是INT不是String:
内部异常:org.postgresql.util.PSQLException:错误:列"entity_type"的类型为整数但表达式的类型为字符不同
但是,如果我@DiscriminatorValue("1")改为@DiscriminatorValue(1),我得到一个编译错误:
类型不匹配:无法从int转换为String
我需要一个整数.有什么快速建议?
在您的@DiscriminatorColumn注释上,您需要指定discriminatorType:
@DiscriminatorColumn(name="entity_type", discriminatorType=DiscriminatorType.INTEGER)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2551 次 |
| 最近记录: |