Search code examples
javahibernatejoinh2in-memory-database

Cannot Use Joins in H2 Database


I am running into a strange issue when executing certain queries via Java Hibernate while using H2 (1.3.168) as the local database. It appears that the Hibernate attempts to execute a series of SELECT statements but one is failing with a vague error.

The log file reports that a few SELECT statements that do get executed successfully only have a single participating table, e.g. SELECT ... FROM T1 WHERE ...;

However, when it gets to a simple LEFT OUTER JOIN statement in the statement, it produces the following error:

General error: "java.lang.NoClassDefFoundError: org/h2/table/TableFilter$2" [50000-168]

I took the exact same query reported by Hibernate and tried to executed it directly in H2 console. Same error.

I replaced LEFT OUTER JOIN with a simple JOIN, same error. I removed the second table from the JOIN and just SELECT from the first table, it SELECTs just fine. Did the same for the second table on its own, no issue, e.g.

SELECT * FROM T1;

Tried bunch of other tables with joins... the error reappears.

SELECT * FROM T1 t1 JOIN T2 t2 ON t1.pk_id = t2.fk_id;

Does anyone know what the issue is with H2?


Solution

  • The issue was with one of the missing sequence objects in the drop-recreate script when the application executed at the start (had nothing to do with Hibernate/Spring). Ran the scripts separately in H2 and compared it with another set of create/drop statements to find out the latter works but not the former.

    It's very strange that H2 falls apart altogether because of this and won't let statements with JOIN in them to get executed while simple queries without JOIN work fine.