7

I have an entity called User which leads to a table called user which in PostgreSQL should be surrounded with quotes for it to work. I know I can specify a custom table name, but shouldn't Hibernate do the quotes automatically?

I was told that maybe Hibernate is not using the PostgreSQL dialect. Is that possible when my database is configured like this:

spring.datasource.url = jdbc:postgresql://localhost/database_name
spring.datasource.username = username
spring.datasource.password = password

and if that's the case, how do I make Hibernate use the correct dialect?

I tried:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

and

spring.jpa.database=postgresql

and

spring.jpa.database-platform = org.hibernate.dialect.PostgreSQL94Dialect

and

spring.jpa.database=org.hibernate.dialect.PostgreSQLDialect

having no effect on the error:

Hibernate: insert into user (created_at, last_modified_at, account_id, email, hashed_password_salt, name, over_hashed_password, preferred_name, public_key, started_displaying_sites_at, watched_tutorial_at, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2017-09-08 14:41:40.177  WARN 15764 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42601
2017-09-08 14:41:40.177 ERROR 15764 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: syntax error at or near "user"
  Position: 13
2

4 Answers 4

9

The issue is not the dialect, but the query using the reserved PostgreSQL word user. You can add the quotes through

@Entity(name = "`user`")

Or rename the user table.

Sign up to request clarification or add additional context in comments.

Comments

9

You can specify the configuration property hibernate.auto_quote_keyword with value true to enable automatic quoting of keywords (assuming those are correctly defined in the dialect and/or JDBC database meta data).

See also Mapping Properties:

hibernate.auto_quote_keyword
true or false (default value)
Specifies whether to automatically quote any names that are deemed keywords.

For Spring Boot, you can set this as spring.jpa.properties.hibernate.auto_quote_keyword=true (thanks Nikita Bosik)

1 Comment

For Spring Boot: spring.jpa.properties.hibernate.auto_quote_keyword=true
-1

adding schema field in @Entity would also solve the problem

Comments

-1

You should put the table name on class mapping in escapes, like this:

@Entity(name = "\"user\"")

Escaping using backticks is a Hibernate specific feature; escaping with double quotes is JPA compliant.

1 Comment

Upvoting, because while it doesn't answer the question, it's related to the (currently) accepted answer mentioning Hibernate specific backticks. That answer doesn't answer the question as well and mentioning that backticks are Hibernate specific is of use. So no need to downvote here too much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.