spring boot 1.5 + hibernate 5不能自动将列camelCase转换为下划线

时间:2017-07-18 08:24:16

标签: hibernate spring-boot

基于Spring boot 1.5和hibernate 5, 我创建了许多实体并配置hibernate.hbm2ddl.auto=update。我想从tables自动生成entities。 但是有一个问题我无法解决。 Hibernate无法自动将列从camelCase转换为下划线。

我看了: http://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html#naming

spring - hibernate 5 naming strategy configuration

ImprovedNamingStrategy no longer working in Hibernate 5

但是这些不能解决我的问题。 请告诉我如何配置hibernate或spring boot?

这是我的BaseEntity类。

@MappedSuperclass
public class BaseEntity implements Serializable {

    @Id
//    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    private String id;

    @Column(nullable = false, updatable = false)
    private Date createDate;

    @Column(nullable = false)
    private Date modifyDate;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    @Override
    public String toString() {
        return "BaseEntity{" +
                "id='" + id + '\'' +
                ", createDate=" + createDate +
                ", modifyDate=" + modifyDate +
                '}';
    }
}

这是我的AdminUser类。

@Entity
@Table(name = "ifox_admin_user")
public class AdminUserEO extends BaseEntity {

    public enum AdminUserEOStatus {
        ACTIVE,
        INVALID
    }

    @Column(nullable = false, length = 30, unique = true)
    private String loginName;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false)
    private AdminUserEOStatus status;

    @Column(nullable = false)
    private Boolean buildinSystem = false;

    private String nickName;

    private String email;

    @Column(length = 20)
    private String mobile;

    private String remark;

    @ManyToMany
    @JoinTable(name = "ifox_admin_user_role", joinColumns = {@JoinColumn(name = "admin_user")}, inverseJoinColumns = {@JoinColumn(name = "role")})
    private List<RoleEO> roleEOList = new ArrayList<>();


    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public AdminUserEOStatus getStatus() {
        return status;
    }

    public void setStatus(AdminUserEOStatus status) {
        this.status = status;
    }

    public Boolean getBuildinSystem() {
        return buildinSystem;
    }

    public void setBuildinSystem(Boolean buildinSystem) {
        this.buildinSystem = buildinSystem;
    }

    public List<RoleEO> getRoleEOList() {
        return roleEOList;
    }

    public void setRoleEOList(List<RoleEO> roleEOList) {
        this.roleEOList = roleEOList;
    }

    @Override
    public String toString() {
        return "AdminUserEO{" +
                "loginName='" + loginName + '\'' +
                ", password='" + password + '\'' +
                ", status=" + status +
                ", buildinSystem=" + buildinSystem +
                ", nickName='" + nickName + '\'' +
                ", email='" + email + '\'' +
                ", mobile='" + mobile + '\'' +
                ", remark='" + remark + '\'' +
                '}';
    }
}

当我启动项目时,我得到ifox_admin_user表,就像这样。 enter image description here 列是camelCase命名。

这是我的application.yml配置。

#==================== Tomcat Setting =====================#
server:
  port: 8080
  tomcat:
    uri-encoding: UTF-8

#==================== sessionFactory Setting =====================#
sessionFactory:
  package.scan: com.ifox.platform.entity

#==================== swagger Setting =====================#
swagger2:
  apis.base.package: com.ifox.platform

#==================== spring Setting =====================#
spring:
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
  mvc:
    static-path-pattern: /**
  http.multipart:
      max-request-size: 30MB
      max-file-size: 30MB
  jpa:
    generate-ddl:
      true
    hibernate:
      ddl-auto:
        update
      naming.implicit-strategy:
        org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
      naming.physical-strategy:
        org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

#==================== logging Setting =====================#
logging.level:
  org.springframework: info
  org.hibernate: info
  com.ifox.platform: debug

#==================== DataSource Setting =====================#
app.datasource:
  jdbc:
    url: jdbc:mysql://localhost:3306/pure_site?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: Aswd1234
    driver: com.mysql.jdbc.Driver
  pool:
    initialSize: 1
    minIdle: 1
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: false
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat


#==================== Hibernate Configuration =====================#
hibernate:
  dialect: org.hibernate.dialect.MySQL5Dialect
  show_sql: true
  hbm2ddl.auto: update
  generate_statistics: true
  max_fetch_depth: 1
  jdbc:
    fetch_size: 30
    batch_size: 30
  cache:
    use_second_level_cache: true
    use_query_cache: false
    provider_class: org.hibernate.cache.NoCacheProvider

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题...... 看起来org.hibernate.dialect.MySQL5Dialect覆盖了命名策略。

将其替换为org.hibernate.dialect.MySQLInnoDBDialect解决了我的问题。

您还可以测试以编程方式设置命名策略,以便覆盖MySql5的命名策略:

configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html#configuration-programmatic