Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 JdbcSQLException: "Table not found" with camelcase table & entity name

Using Spring Boot, with Spring Data JPA and H2 in-memory database (in PostgreSQL mode if it makes a difference).

I have a table & entity class named ContentBlock, yet H2 is complaining about missing CONTENT_BLOCK table, when I do a findAll() or findOne():

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

I'm not sure if uppercase/camelcase makes a difference, but where does the underscore in CONTENT_BLOCK come from?

In my schema definition:

CREATE TABLE ContentBlock (
  id       BIGSERIAL PRIMARY KEY,
  content  TEXT
  -- etc
);

And in the entity class:

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

(Of course I first tried without @Table annotation, as the class name exactly matches the table name.)

With my other tables/entities, with names like Asset, there are no problems, and I don't need to explicitly specify the table name on Java side:

@Entity   
public class Asset {
    // ...
}

In my setup, the H2 datasource is explicitly defined like this:

@Bean
public DataSource devDataSource() {  
        return new EmbeddedDatabaseBuilder()
          .generateUniqueName(true)
          .setType(EmbeddedDatabaseType.H2)
          .setScriptEncoding("UTF-8")
          .ignoreFailedDrops(true)
          .addScripts("database/init.sql", "database/schema.sql", "database/test_data.sql")
          .build();
    }

(Contents of init.sql is SET MODE PostgreSQL;)

As workaround, I just renamed the ContentBlock table to Block in schema.sql, with @Table(name = "Block") in the Java class which I still call ContentBlock.

But this is weird, surely you can map a table with camelcase name to an entity somehow?

like image 830
Jonik Avatar asked Dec 11 '25 23:12

Jonik


1 Answers

By default Spring Boot uses SpringNamingStrategy. It extends org.hibernate.cfg.ImprovedNamingStrategy from Hibernate 4. ImprovedNamingStrategy generates underscores in table names.

To map a table with camel case name to an entity you can use org.hibernate.cfg.EJB3NamingStrategy or implement your own.

An example of set a name strategy using properties

spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
like image 137
v.ladynev Avatar answered Dec 14 '25 05:12

v.ladynev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!