JPA Hibernate 驼峰式字段
Posted
技术标签:
【中文标题】JPA Hibernate 驼峰式字段【英文标题】:JPA Hibernate camelcase field 【发布时间】:2016-06-12 08:22:05 【问题描述】:我有 spring 4+ hibernate 4.3 + psql 9.5 应用程序,遇到了一些驼峰式字段转换的问题。
Hibernate: select trainservi0_.trainServiceId as trainSer1_16_, trainservi0_.category as category2_16_, trainservi0_.date as date3_16_, trainservi0_.description as descript4_16_, trainservi0_.name as name5_16_, trainservi0_.status as status6_16_, trainservi0_.trainNumber as trainNum7_16_, trainservi0_.version as version8_16_ from tms.public.train_service trainservi0_
2016-02-29 13:09:34 WARN SqlExceptionHelper:144 - SQL Error: 0, SQLState: 42703
2016-02-29 13:09:34 ERROR SqlExceptionHelper:146 - ERROR: column trainservi0_.trainserviceid does not exist
Perhaps you meant to reference the column "trainservi0_.trainServiceId".
这是我的配置:
public class DataBaseProdConfig
@Resource
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(env.getRequiredProperty("db.entity.package"));
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.setJpaProperties(getHibernateProperties());
return em;
@Bean
public DataSource dataSource()
BasicDataSource ds = new BasicDataSource();
ds.setUrl(env.getRequiredProperty("db.url"));
ds.setDriverClassName(env.getRequiredProperty("db.driver"));
ds.setUsername(env.getRequiredProperty("db.username"));
ds.setPassword(env.getRequiredProperty("db.password"));
ds.setInitialSize(Integer.valueOf(env.getRequiredProperty("db.initialSize")));
ds.setMinIdle(Integer.valueOf(env.getRequiredProperty("db.minIdle")));
ds.setMaxIdle(Integer.valueOf(env.getRequiredProperty("db.maxIdle")));
ds.setTimeBetweenEvictionRunsMillis(Long.valueOf(env.getRequiredProperty("db.timeBetweenEvictionRunsMillis")));
ds.setMinEvictableIdleTimeMillis(Long.valueOf(env.getRequiredProperty("db.minEvictableIdleTimeMillis")));
ds.setTestOnBorrow(Boolean.valueOf(env.getRequiredProperty("db.testOnBorrow")));
ds.setValidationQuery(env.getRequiredProperty("db.validationQuery"));
return ds;
@Bean
public PlatformTransactionManager transactionManager()
JpaTransactionManager manager = new JpaTransactionManager();
manager.setEntityManagerFactory(entityManagerFactory().getObject());
return manager;
public Properties getHibernateProperties()
try
Properties properties = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream("hibernate.properties");
properties.load(is);
return properties;
catch (IOException e)
throw new IllegalArgumentException("Can't find 'hibernate.properties' in classpath!", e);
这是我的实体:
@Entity
@Table(name = "train_service", schema = "public", catalog = "tms")
public class TrainServiceEntity
private Long trainServiceId;
@Id
@SequenceGenerator(name="train_service_trainServiceId_seq",
sequenceName="train_service_trainServiceId_seq",
allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator="train_service_trainServiceId_seq")
@Column(name = "trainServiceId")
public Long getTrainServiceId()
return trainServiceId;
public void setTrainServiceId(Long trainServiceId)
this.trainServiceId = trainServiceId;
....
所以 Hibernate 将 'trainServiceId' 转换为 'trainserviceid' ,但在日志中我看到了正确的字段名称:
Hibernate: select trainservi0_.trainServiceId as trainSer1_16_, ...
我需要做什么,强制 Hibernate 不将字段名称转换为小写?
【问题讨论】:
实体的列名如果需要大写,需要设置为大写。 我不想要大写,我不会使用与数据库中相同的驼峰式 “所以 Hibernate 将 'trainServiceId' 转换为 'trainserviceid' ,但在日志中我看到了正确的字段名称” 我不明白,你有问题还是不是?你怎么知道错误的语句被发送到数据库?来自数据库日志? 所以当我尝试执行 findAll() 语句时,我在日志中看到错误,因为我设置了“hibernate.show_sql = true”。请查看我的问题标题中的日志 【参考方案1】:您可以尝试将大小写混合的列名称放在连字符中。
@Column("\"trainServiceId\"")
参见persistence tables,搜索“带有特殊字符和大小写混合的表格”
【讨论】:
以上是关于JPA Hibernate 驼峰式字段的主要内容,如果未能解决你的问题,请参考以下文章
spring boot 中Spring data jpa数据库表字段命名策略
Hibernate/JPA:如何强制隐式连接使用 LEFT OUTER JOINS
Hibernate/JPA - 使用@Future 更新还包含日期字段的实体字段