欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot JPA 取消小驼峰到下划线映射,JPA取值部分字段为NULL的问题

程序员文章站 2022-04-23 16:01:49
...

springboot JPA 取消小驼峰到下划线映射,JPA取值部分字段为NULL的问题

这种问题多半是因为数据库字段是小驼峰命名,虽然实体字段和数据库字段一致,但是 JPA 的生成的sql语句还是把小驼峰转化为 下划线了所以查不到;

#错误生成sql
select `appointmen0_`.id as id1_0_, appointmen0_.appointment_code as appointm2_0_, appointmen0_.appointment_time as appointm3_0_, appointmen0_.arrival_time as arrival_4_0_, appointmen0_.car_licence as car_lice5_0_, appointmen0_.car_owner_name as car_owne6_0_, appointmen0_.contact_type as contact_7_0_, appointmen0_.create_time as create_t8_0_, appointmen0_.shop_name as shop_nam9_0_, appointmen0_.type as type10_0_ from appointment appointmen0_

解决办法:

该配置文件:
yml:

spring:
  application:
    name: WashProvider
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://xxxxxx
    username: root
    password: xxx
    type: com.alibaba.druid.pool.DruidDataSource
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
      naming:
        # 取消小驼峰到下划线映射(加上这个)
        implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

properties:

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

配完之后就正常了

select appointmen0_.id as id1_0_, appointmen0_.appointmentCode as appointm2_0_, appointmen0_.appointmentTime as appointm3_0_, appointmen0_.arrivalTime as arrivalT4_0_, appointmen0_.carLicence as carLicen5_0_, appointmen0_.carOwnerName as carOwner6_0_, appointmen0_.contactType as contactT7_0_, appointmen0_.createTime as createTi8_0_, appointmen0_.shopName as shopName9_0_, appointmen0_.type as type10_0_ from appointment appointmen0_