You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Hibernate表名小写问题求助:需始终使用大写表/列名

Hey there! Let's break down why you're hitting that ERROR: relation "nickname" does not exist error with PostgreSQL, Spring Boot, and Hibernate—this is a super common gotcha with Postgres's identifier handling, especially when you want to use uppercase table/column names.

First, Understand the Root Cause

PostgreSQL has a key quirk with identifier case sensitivity:

  • When you create a table without double quotes (e.g., CREATE TABLE NICKNAME (...)), Postgres automatically converts the table name to lowercase and stores it as nickname.
  • When you reference the table in SQL without quotes (e.g., SELECT * FROM NICKNAME), Postgres converts that to nickname too.
  • But if you created your table with double quotes (e.g., CREATE TABLE "NICKNAME" (...)), Postgres preserves the uppercase—and you must use quotes to reference it, otherwise Postgres will look for a lowercase nickname table (which doesn't exist).

Your entity uses @Table(name="NICKNAME"), but Hibernate is probably generating SQL without quotes, so Postgres is looking for nickname instead of your uppercase NICKNAME table.

Fixes to Force Uppercase Table/Column Names

Here are the most reliable ways to make Hibernate always use uppercase identifiers with Postgres:

1. Configure a Global Hibernate Naming Strategy (Recommended)

This is the cleanest approach—set up a custom naming strategy that converts all table/column names to uppercase and tells Hibernate to use quotes when needed.

First, create a custom physical naming strategy class:

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;

public class UpperCasePhysicalNamingStrategy extends PhysicalNamingStrategyStandardImpl {

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        // Convert table name to uppercase and enforce quoting
        return Identifier.toIdentifier(name.getText().toUpperCase(), true);
    }

    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
        // Same logic for column names
        return Identifier.toIdentifier(name.getText().toUpperCase(), true);
    }
}

Then register this strategy in your application.properties:

# Use your custom uppercase naming strategy (replace with your actual package path)
spring.jpa.hibernate.naming.physical-strategy=com.your.project.package.UpperCasePhysicalNamingStrategy

# Optional: Disable implicit naming to avoid conflicts with default rules
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

This will make Hibernate generate SQL like INSERT INTO "NICKNAME" (...) VALUES (...), which Postgres will correctly match to your uppercase table.

2. Explicitly Add Double Quotes in Entity Annotations

If you don't want to create a custom strategy, you can manually add escaped double quotes to your @Table and @Column annotations:

@Entity
@Table(name="\"NICKNAME\"") // Escaped double quotes to preserve uppercase
public class NickName {

    @Id
    @Column(name="\"ID\"")
    private Long id;

    @Column(name="\"NICK_NAME\"")
    private String nickName;

    // Getters and setters
}

This works, but it's tedious if you have many entities or columns.

3. Verify Your Actual Database Table Name

Before making any code changes, confirm what your table is actually named in Postgres. Run this query in your Postgres client:

SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';

If the table shows up as nickname (lowercase), you can either:

  • Rename it to uppercase with ALTER TABLE nickname RENAME TO "NICKNAME";
  • Or adjust your entity's @Table name to nickname (lowercase) if you don't strictly need uppercase identifiers.

Quick Test

After applying one of these fixes, try pushing data again—Hibernate should now correctly reference your uppercase table, and the error should disappear.

内容的提问来源于stack exchange,提问作者theRoot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:41:58