3

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?

Jonik
  • 80,077
  • 70
  • 264
  • 372
  • In reality there are other columns as well, such as foreign key reference to another table, but that is not relavant here; I can reproduce this problem with a simplistic setup like above. – Jonik Nov 04 '15 at 11:40
  • Maybe put (single) quotes around the names? worth a try perhaps. Some RDBMS use quoting as a way of allowing case sensitivity – Neil Stockton Nov 05 '15 at 07:11
  • @NeilStockton: Hmm, using single quotes in the CREATE TABLE statement produces a syntax error, but with double quotes I actually get `Table "CONTENTBLOCK" not found` (no underscore!), so you may be onto something here. – Jonik Nov 05 '15 at 09:43
  • 1
    I think this might be solvable using double quotes *and* [`DATABASE_TO_UPPER=false`](http://stackoverflow.com/a/17925668/56285). But I'm not sure how to specify that, since I don't explicitly use a H2 connection URL anywhere in the app... – Jonik Nov 05 '15 at 09:48

1 Answers1

3

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
v.ladynev
  • 19,275
  • 8
  • 46
  • 67