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




