Search code examples
javahibernatejpahsqldb

JPA HSQL does not fetch fields from extended classes


I am inserting into HSQL following data:

INSERT INTO Consumer(id,ConsumerId,CREATEDBY,CREATEDAT,FK_Service,ConsumerType) VALUES(300,'adsgvcg-cvvce-hvchwec','sm',CURRENT_TIMESTAMP,1,'INTERNAL');

I have the following Classes which read from the table:

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

and the base class looks like:

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

When I am running the code, I am getting the following exception:

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

However when I move the fields from the base class to the parent (Consumer) class, this exception no longer occurs and the fields are getting populated as well.


Solution

  • You need to use an inheritance strategy in hibernate In your base entity, you need to add an annotation so that hibernate knows which strategy to use.

    @MappedSuperclass

    or

    @Inhertitance(strategy=InheritanceType.SINGLE_TABLE) you can use one of these strategies :

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

    This wikibook page can be helpful.