JPA HSQL 不从扩展类中获取字段
Posted
技术标签:
【中文标题】JPA HSQL 不从扩展类中获取字段【英文标题】:JPA HSQL does not fetch fields from extended classes 【发布时间】:2015-07-25 18:43:02 【问题描述】:我正在向 HSQL 中插入以下数据:
INSERT INTO Consumer(id,ConsumerId,CREATEDBY,CREATEDAT,FK_Service,ConsumerType) VALUES(300,'adsgvcg-cvvce-hvchwec','sm',CURRENT_TIMESTAMP,1,'INTERNAL');
我有以下从表中读取的类:
Entity(name = "Consumer")
@Table(name = "Consumer")
public class Consumer extends Base
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "ID")
private long id;
@Basic(optional = false)
@Column(name = "ConsumerId", nullable = false)
private String ConsumerId;
@Basic(optional = false)
@Column(name = "ConsumerType", nullable = false)
private String ConsumerType;
@Basic//(optional = false)
@Column(name = "FK_Service", nullable = true)
private String service;
//Getter Setters etc
基类看起来像:
public class Base implements Serializable
@Basic
@Column(name = "MODIFIEDBY", nullable = true)
private String ModifiedBy;
@Basic
@Column(name = "MODIFIEDAT", nullable = true)
private Timestamp ModifiedAt;
@Basic
@Column(name = "CREATEDBY", nullable = true)
private String CreatedBy;
@Basic
@Column(name = "CREATEDAT", nullable = true)
private Timestamp CreatedAt;
//Getter Setters etc
当我运行代码时,出现以下异常:
2015-07-25 22:21:12,875 ERROR [main] hbm2ddl.SchemaExport.? (?:?) - HHH000388: Unsuccessful: INSERT INTO Consumer(id,ConsumerId,CREATEDBY,CREATEDAT,FK_Service,ConsumerType) VALUES(300,'adsgvcg-cvvce-hvchwec','smenon2',CURRENT_TIMESTAMP,1,'INTERNAL')
2015-07-25 22:21:12,875 ERROR [main] hbm2ddl.SchemaExport.? (?:?) - user lacks privilege or object not found: CREATEDBY
但是,当我将字段从基类移动到父(消费者)类时,此异常不再发生,并且字段也被填充。
【问题讨论】:
在基类中尝试@MappedSuperclass 【参考方案1】:你需要在休眠中使用inheritance strategy 在您的基础实体中,您需要添加一个注解,以便 hibernate 知道要使用哪种策略。
@MappedSuperclass
或
@Inhertitance(strategy=InheritanceType.SINGLE_TABLE) 你可以使用以下策略之一:
InheritanceType.SINGLE_TABLE InheritanceType.TABLE_PER_CLASS InheritanceType.JOINED此wikibook page 可能会有所帮助。
【讨论】:
以上是关于JPA HSQL 不从扩展类中获取字段的主要内容,如果未能解决你的问题,请参考以下文章
Hibernate 4、JPA 2 和 HSQL,无法通过 ID 查询