"Version" table name is not escaped with SchemaExport

I am using SchemaExport with auto_quote_keyword. There is an entity called “Version”, which is not quote escaped by the generated SQL, but it seems be escaped with PostgreSQL10Dialect at run time, giving the error

Caused by: org.postgresql.util.PSQLException: ERROR: relation “Version” does not exist

On turning on the ddl-auto, the SQL has

Hibernate: drop table if exists “Version” cascade
Hibernate: create table “Version” (id int4 not null, “version” varchar(255) not null, primary key (id))

The usage of SchemaExport is:

    private static void saveInitialScript(Path path, Class<? extends Dialect> dialect) throws IOException {
        var builder = new StandardServiceRegistryBuilder();
        builder.applySetting("hibernate.dialect", dialect);
        builder.applySetting("hibernate.auto_quote_keyword", true);
        var metadata = new MetadataSources(builder.build());

        for (var c : ENTITIES) {
        var export = new SchemaExport();

        export.createOnly(EnumSet.of(TargetType.SCRIPT), metadata.buildMetadata());

Can you be a bit more specific? Which statement is showing this problem? According to the DDL you posted, it seems that quoting is done, no?

Thanks for your response, and sorry if I wasn’t clear enough.

The actual case:

  • I am using SchemaExport to generate the sql creation script, used with auto_quote_keyword. The script has create table Version (...) without quotes. When using the Spring, there is an error, because the generated queries can’t find this table

Hibernate: select version0_.id as id1_0_, version0_.“version” as version2_0_ from “Version” version0_

However, if ddl-auto is set to create, everything is fine, because the created table in this case is “Version”.

So, this issue is with the SchemaExport to handle table names if they should be quoted.

Hope that clarifies the issue.

You obviously have to provide the correct dialect to the metadata configuration as that is what drives the quoting rules. Which dialect are you passing? You can check what happens in org.hibernate.tool.schema.internal.StandardTableExporter#getSqlCreateStrings

Yes, the correct dialect is used, org.hibernate.dialect.PostgreSQL10Dialect, which is assigned to StandardTableExporter.dialect.

In StandardTableExporter.getSqlCreateStrings(), the first statement returns tableName as Version, without the quotes.

Version used: hibernare-core 5.6.1

You will have to debug a bit to find out what the problem is. Set a breakpoint in org.hibernate.mapping.Table#setName and try to figure out why the identifier does not get passed in as quoted.