|
I had exactly the same issue while using hibernate.globally_quoted_identifiers=true with Postgres9. The workaround I have is to set hibernate.globally_quoted_identifiers=false.
If I understand it correctly the logic is next.
When globally_quoted_identifiers is set to false it runs org.hibernate.tool.hbm2ddl.DatabaseMetadata.initSequences method at line 169, then it runs into line 171 String sql = dialect.getQuerySequencesString(); and returns query select relname from pg_class where relkind='S' which seems to be ok. Based on the results of that query it populates sequences object with the values at line 181 in a loop sequences.add(rs.getString(1).toLowerCase(Locale.ROOT).trim());. So sequences object is populated with values like [ users_seq, roles_seq ]
Then it runs into org.hibernate.cfg.Configuration.iterateGenerators method at line 918. After that it runs into org.hibernate.cfg.Configuration.generateSchemaUpdateScriptList where the last step is to iterate through generators at line 1304. And here where the issue starts: condition if ( !databaseMetadata.isSequence( key ) && !databaseMetadata.isTable( key ) ) does not work correctly in case of hibernate.globally_quoted_identifiers=true however it works fine for hibernate.globally_quoted_identifiers=false. The key value is "app"."users_seq" and databaseMetadata.isSequence( key ) returns false while it has to return true like this would be without quotes - databaseMetadata.isSequence( "app.users_seq" ) instead of databaseMetadata.isSequence( "\"app\".\"users_seq\"" ). As you can see the issue is with the quotes and may be related to another opened issue HHH-8574. And it must not run into line 1309 since I already have the sequence with that name but it cannot be found due to quotes.
The query mentioned in description of this issue is related to databaseMetadata.isTable method invocation, that is why the query must not be fixed since it will break isTable method. So I believe the query is fine and it runs as a fallback because it could not find sequence due to quotes bug.
In case of globally_quoted_identifiers is set to false it is able to find all sequences and does not run the query you want to fix.
So in my opinion the fix has to be done inside isSequence method of org.hibernate.tool.hbm2ddl.DatabaseMetadata class. The logic of isSequence method has to be smart about quotes at lines 198 and 199:
String[] strings = StringHelper.split(".", (String) key);
return sequences.contains( strings[strings.length-1].toLowerCase(Locale.ROOT));
Since hibernate.globally_quoted_identifiers=false works for me as a solution for now I'm not going to make any code changes as for now. Anybody who needs quotes turned on please go ahead or you can wait for my contribution until I need it )))
|