PSQLException: column user0_.id does not exist in a Hibernate query
org.postgresql.util.PSQLException: ERROR: column user0_.id does not existShort answer
Hibernate generated SQL that names a column PostgreSQL does not have under that exact name. The three usual causes are a naming rule that maps your field to a different name than the real column, a column created with quotes and mixed case, and a table that was never updated. Read the SQL in the log and compare it with the table.
Fix it fast
1Find out which cause you have
Print the SQL Hibernate sends and list the columns the table really has. The name in the error and a name in the list must be identical, letter by letter.
# application.properties spring.jpa.show-sql=true # shell psql -d mydb -c '\d users'2Match what you see, then jump to its fix
- 1
The log shows user_id while the table has a differently spelled column, or the other way roundSpring Boot's naming rule maps the field to a different column nameFix - 2
\d shows a name such as "userId" with capital lettersThe column was created with quotes and mixed caseFix - 3
A recently added field is the one the error names, and the app uses a real PostgreSQL databaseThe column was added to the entity but never to the databaseFix
- 1
What does the error look like?
org.postgresql.util.PSQLException: ERROR: column user0_.id does not exist
Cause 1 of 3
Spring Boot's naming rule maps the field to a different column name
1Check
By default Spring Boot uses CamelCaseToUnderscoresNamingStrategy, so a field called userId is mapped to the column user_id. The table must use the same name.
psql -d mydb -c '\d users'2Fix
Name the column in the mapping, or move to Hibernate's own naming so names are used as written.
@Column(name = "user_id")
private Long userId;
# or, to use names as written
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl3Verify
Repeat the failing query.
curl -i http://localhost:8080/users/1Confirmed by docs.spring.io
Cause 2 of 3
The column was created with quotes and mixed case
1Check
PostgreSQL folds unquoted names to lower case. A name written in quotes keeps its case and is a different identifier from the lower-case one, so SQL that asks for userid does not match a column created as "userId".
psql -d mydb -c "SELECT column_name FROM information_schema.columns WHERE table_name = 'users'"2Fix
Rename the column to the lower-case name the mapping produces.
ALTER TABLE users RENAME COLUMN "userId" TO user_id;3Verify
The column list now shows the lower-case name and the query runs.
psql -d mydb -c '\d users'Confirmed by postgresql.org
Cause 3 of 3
The column was added to the entity but never to the database
1Check
Spring Boot sets spring.jpa.hibernate.ddl-auto to none for a non-embedded database unless a schema manager is in use, so nothing adds the new column for you.
grep -rn "ddl-auto" src/main/resources/2Fix
Add the column with a migration, using the name your mapping produces.
ALTER TABLE users ADD COLUMN IF NOT EXISTS display_name varchar(100);3Verify
Restart the app and repeat the request.
psql -d mydb -c '\d users'Confirmed by postgresql.org
How do I stop it coming back?
- Keep schema changes in migrations (Flyway or Liquibase) that ship with the code change that needs them.
- Create columns with unquoted lower-case snake_case names.
- Run integration tests against PostgreSQL, so naming differences fail in CI.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Databases
Spring Boot JPA cannot find the PostgreSQL table it maps to
ERROR: relation "telephone_number" does not exist
Spring Boot maps an entity to a lowercase snake_case table name and creates tables on its own only for embedded databases.
2 causes, each with a fixDatabases
Flyway cannot handle the PostgreSQL URL after upgrading to Flyway 10 or newer
No database found to handle jdbc:postgresql://localhost:5432/app
Since Flyway 10, each database has its own module.
4 causes, each with a fixDatabases
H2 in PostgreSQL mode still behaves differently from PostgreSQL
Names or null ordering differ from PostgreSQL in test results
H2 compatibility modes are switched on in the JDBC URL and cover only a small part of the differences between databases.
4 causes, each with a fixDatabases
Hibernate cannot find the sequence hibernate_sequence on PostgreSQL
ERROR: relation "hibernate_sequence" does not exist
Hibernate 6 no longer uses one shared hibernate_sequence.
3 causes, each with a fixDatabases
Spring Boot fails on startup: HikariPool-1 exception during pool initialization
Caused by: ... (the underlying driver exception)
HikariCP opens one connection when the pool starts, and the startup fails when that connection fails.
3 causes, each with a fixDatabases
PostgreSQL cannot work out the type of a null query parameter from Hibernate
org.postgresql.util.PSQLException: ERROR: function lower(bytea) does not exist
PostgreSQL works out an unspecified parameter's type from the place where it is first used.
3 causes, each with a fix