Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the correct dialect, PostgreSQL, on Spring Boot

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
like image 784
pupeno Avatar asked Jul 16 '26 10:07

pupeno


1 Answers

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.

like image 113
Albert Bos Avatar answered Jul 18 '26 23:07

Albert Bos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!