0

Base on spring boot 1.5 and hibernate 5, I created many entities and configure hibernate.hbm2ddl.auto=update. I want to generate tables automatically from entities. But there is a problem that I can't resolve. Hibernate can't automatically convert columns from camelCase to underscore.

I read : 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

but above these can't resolve my problem. Please tell me how to configure hibernate or spring boot?

This is my BaseEntity class.

@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 +
                '}';
    }
}

This is my AdminUser class.

@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 + '\'' +
                '}';
    }
}

When I launch project, I get ifox_admin_user table, like this. enter image description here The columns are camelCase naming.

This is my application.yml configuration.

#==================== 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
Yeager
  • 57
  • 1
  • 1
  • 6

1 Answers1

0

I was facing the same problem... It looks org.hibernate.dialect.MySQL5Dialect is overriding the naming strategy.

Replace it by org.hibernate.dialect.MySQLInnoDBDialect solved the problem for me.

You can also test to set up your Naming Strategy programmatically, so the MySql5's naming strategy will be overrided :

configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);

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

Karbos 538
  • 2,977
  • 1
  • 23
  • 34