Search code examples
javaunit-testingpersistencehsqldbin-memory-database

Java Entity can't be persisted in the script created table in HSQLDB


I have an application which is using Oracle DB. For the Unit testing I'm using HSQLDB.

I have created PRODUCT table with script in the HSQLDB in this way:

CREATE TABLE PRODUCT
(
    ID INTEGER IDENTITY PRIMARY KEY,
    NAME VARCHAR (73)
);

I have an Entity:

@Entity
@Table(name = "PRODUCT")
public class Product implements Serializable {
    @Id
    @Column(name = "ID", nullable = false)
    private int id;

    @Column(name = "NAME")
    private String name;

    ... Getters, Setters...
}

The problem is when I'm trying to persist a Product (of course after I created the table) I'm getting this error:

javax.persistence.RollbackException: Error while committing the transaction
...
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: PRODUCT
...

Do you have any idea why the java entity doesn't see the table? Thank you in advice!


Solution

  • The usual cause of this is the database URL in your app is not correct. You create the table in one database, but your app is pointing to another database, which is empty. Use an absolute path, or a path that is based on the user's home directory, or a directory path that is a system property.

    http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html#dpc_variables_url