Home Table not found with H2
Reply: 2

Table not found with H2

pecka
1#
pecka Published in 2018-01-10 20:06:56Z

My Java EE application cannot find tables. I am using WildFly (as the application server) and H2 (as the DB, in the embedded mode).

The error is:

org.h2.jdbc.JdbcSQLException: Table "MY_TABLE" not found

Look at my table creation:

create table "MY_TABLE" (
    -- ...
);

See how my entity is defined:

@Entity
@Table(name = "MY_TABLE")
public class MyTable {
    // ...
}

This is how I call JPA (this causes the exception):

@PersistenceContext
private EntityManager entityManager;

// ...

entityManager.find(MyTable.class, 1);

My persistence.xml is:

<persistence-unit name="myapp" transaction-type="JTA">
    <jta-data-source>java:jboss/datasources/myappDS</jta-data-source>
</persistence-unit>

And the standalone.xml in my WildFly:

<datasource jndi-name="java:jboss/datasources/myappDS" pool-name="myappDS" enabled="true" use-java-context="true">
    <connection-url>jdbc:h2:~/myapp;SCHEMA=PUBLIC</connection-url>
    <driver>h2</driver>
</datasource>
<drivers>
    <driver name="h2" module="com.h2database.h2">
        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
    </driver>
</drivers>

Everything looks good so why the exception? Do I need to flush something? Or set schema somewhere?

If I configure another datasource (having the same DB structure) in WildFly (for example Postgres), everything works fine. That would mean that the datasource configuration is the place causing the error.

(Yes, I am totally sure the DB is not empty and the connection URL is correct as I have tried it from an SQL client.)

Per Huss
2#
Per Huss Reply to 2018-01-11 10:25:59Z

Are you running the application as a different user than you are testing to connect with? In that case the the ~ in the connection path will resolve to different home folders, and thus different databases.

Otherwise I would suggest connecting with the Shell in the h2 jar file and run show tables to verify that the table exists and with the correct casing. Start the shell by running:

java -cp h2*.jar org.h2.tools.Shell
Georg Henkel
3#
Georg Henkel Reply to 2018-01-11 10:37:08Z

Where do you store your script for the table creation? Is it in classpath of the application?

In my test setup I let hibernate generate the tables on startup (and dropping it on shutdown) and using an import.sql script for the test-data generation, which is in the folder src/main/resources.

<persistence-unit name="myapp" transaction-type="JTA">
  <jta-data-source>java:jboss/datasources/myappDS</jta-data-source>
  <properties>
    <property name="hibernate.hbm2ddl.auto" value="create-drop" />
  </properties>
</persistence-unit>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.349845 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO