Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

PSQLException: column user0_.id does not exist in a Hibernate query

Errororg.postgresql.util.PSQLException: ERROR: column user0_.id does not exist

Short 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.

  • Applies to Hibernate ORM 6.6, Spring Boot 3.4, PostgreSQL 17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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'
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2\d shows a name such as "userId" with capital lettersThe column was created with quotes and mixed caseFix
    3. 3A 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

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.PhysicalNamingStrategyStandardImpl

3Verify

Repeat the failing query.

curl -i http://localhost:8080/users/1

Confirmed by docs.spring.io

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.