JPA HSQL不从扩展类中获取字段

时间:2015-07-25 18:43:03

标签: java hibernate jpa hsqldb

我在数据后插入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

但是,当我将字段从基类移动到父类(Consumer)类时,不再出现此异常,并且字段也会被填充。

1 个答案:

答案 0 :(得分:1)

您需要在休眠中使用inheritance strategy 在基础实体中,您需要添加注释,以便hibernate知道要使用哪种策略。

@MappedSuperclass

@Inrtrtitance(strategy = InheritanceType.SINGLE_TABLE)您可以使用以下策略之一:

  • InheritanceType.SINGLE_TABLE
  • InheritanceType.TABLE_PER_CLASS
  • InheritanceType.JOINED

wikibook page可能会有所帮助。