You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Hibernate-PostgreSQL属性访问配置咨询:实现小写属性的驼峰式访问

Hey there! Let's fix this issue where you can't access PostgreSQL's lowercase columns using camelCase fields in your Hibernate entities. I've dealt with this exact problem before—PostgreSQL treats unquoted column names as lowercase by default, while Hibernate's default naming strategy might not map camelCase fields to those lowercase snake_case columns correctly. Here are your best options:

1. Global Configuration (Most Efficient for Consistent Mapping)

This is my go-to solution because it applies to all entities without needing to annotate every field.

For Spring Boot Projects

Add these lines to your application.properties (or application.yml):

# Maps camelCase entity fields to snake_case lowercase columns (matches PostgreSQL's default)
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
# Optional: Ensure implicit naming follows JPA standards
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

The SpringPhysicalNamingStrategy automatically converts camelCase (like userName) to snake_case lowercase (like user_name), which aligns perfectly with PostgreSQL's default column naming.

For Pure Hibernate (Non-Spring)

Update your hibernate.cfg.xml with these properties:

<!-- Convert entity field names to logical snake_case names -->
<property name="hibernate.implicit_naming_strategy">org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl</property>
<!-- Convert logical names to lowercase for PostgreSQL -->
<property name="hibernate.physical_naming_strategy">org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl</property>
2. Per-Entity/Field Annotation (For Edge Cases)

If you only need to map specific fields or want explicit control, use @Column to specify the exact lowercase column name from your database:

@Entity
@Table(name = "users") // Explicitly map to the lowercase table name
public class User {
    @Id
    @Column(name = "user_id") // Maps to PostgreSQL's `user_id` column
    private Long userId;

    @Column(name = "email_address") // Maps to `email_address`
    private String emailAddress;

    // Getters, setters, and other fields...
}

This is great for one-off cases where your column names don't follow a consistent snake_case pattern.

3. Custom Naming Strategy (For Advanced Customization)

If you need full control over how names are converted (e.g., special prefixes/suffixes), create a custom physical naming strategy:

import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import com.google.common.base.CaseFormat; // Uses Guava, but you can write your own converter

public class PostgresCustomNamingStrategy extends PhysicalNamingStrategyStandardImpl {
    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
        // Convert camelCase to snake_case and force lowercase
        String convertedName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name.getText());
        return Identifier.toIdentifier(convertedName);
    }
}

Then register it in your configuration:

  • Spring Boot: Add to application.properties:
    spring.jpa.hibernate.naming.physical-strategy=com.yourpackage.PostgresCustomNamingStrategy
    
  • Pure Hibernate: Add to hibernate.cfg.xml:
    <property name="hibernate.physical_naming_strategy">com.yourpackage.PostgresCustomNamingStrategy</property>
    

If you don't want to use Guava, you can implement a simple camelCase-to-snake_case converter with regex or string manipulation.

Quick Note

Remember: PostgreSQL treats unquoted column names as lowercase. If your columns were created with quoted identifiers (e.g., "UserName"), you'll need to match that exact case in your mappings—but based on your question, this probably isn't the case here.

内容的提问来源于stack exchange,提问作者Venu Madhav Reddy

火山引擎 最新活动