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) {
metadata.addAnnotatedClass(c);
}
var export = new SchemaExport();
Files.deleteIfExists(path);
export.setOutputFile(path.toString());
export.setDelimiter(";");
export.setFormat(true);
export.createOnly(EnumSet.of(TargetType.SCRIPT), metadata.buildMetadata());
}
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.
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
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.