beskar
beskar

Reputation: 75

Hibernate creating new columns on existing table running with springboot

On running my springboot application hibernate is creating new columns. Below is my entity class

@Entity
@Table(name="contst_games")
public class ContestGames {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="cntstName")
    private String contestName;

    @Column(name="cntstType")
    private String contestType;

    @Column(name="phne")
    private int phoneNumbr;

    // getters and setters

    @Override
    public String toString() {
        return "ContestGames [id=" + id + ", contestName=" + contestName + ", contestType=" + contestType + "]";
    }

}

Below is the setup of properties

# ==========================================================================
# = JPA / HIBERNATE
# ==========================================================================
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql = true

#60 sec
spring.datasource.hikari.connection-timeout=60000
# max 5
spring.datasource.hikari.maximum-pool-size=5
spring.jpa.hibernate.ddl-auto=update
#============================================================================

Below is the newly created table

'id', 'int(11)', 'NO', 'PRI', NULL, 'auto_increment'
'cntstName', 'varchar(255)', 'YES', '', NULL, ''
'cntstType', 'varchar(255)', 'YES', '', NULL, ''
'phne', 'int(11)', 'YES', '', NULL, ''
'cntst_name', 'varchar(255)', 'YES', '', NULL, ''
'cntst_type', 'varchar(255)', 'YES', '', NULL, ''

Below is the alter query that is being run on running the application.

2019-11-28 13:09:23.092  INFO 10615 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2019-11-28 13:09:23.158  INFO 10615 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.4.8.Final}
2019-11-28 13:09:23.344  INFO 10615 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2019-11-28 13:09:23.446  INFO 10615 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-11-28 13:09:23.450  WARN 10615 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2019-11-28 13:09:23.669  INFO 10615 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-11-28 13:09:23.688  INFO 10615 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Hibernate: alter table contst_games add column cntst_name varchar(255)
Hibernate: alter table contst_games add column cntst_type varchar(255)
2019-11-28 13:09:24.867  INFO 10615 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2019-11-28 13:09:24.891  INFO 10615 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-11-28 13:09:25.482  WARN 10615 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning

Two new columns cntst_name and cntst_type are being created at runtime. Ideally no new column should be added since the table already exists and spring.jpa.hibernate.ddl-auto=update. Also to be noted no new column for phone number is being created.

Upvotes: 1

Views: 2450

Answers (2)

Imane B
Imane B

Reputation: 1

if you compare :

'cntstName', 'varchar(255)', 'YES', '', NULL, ''

'cntstType', 'varchar(255)', 'YES', '', NULL, ''

with :

'cntst_name', 'varchar(255)', 'YES', '', NULL, ''

'cntst_type', 'varchar(255)', 'YES', '', NULL, ''

you recognize that the uppercase is the problem, you should made all fields with lowercase.

Upvotes: 0

Helena Meadows
Helena Meadows

Reputation: 11

That's because of Hibernate&Spring Boot Naming Strategies. (more info: https://www.jpa-buddy.com/blog/hibernate-naming-strategies-jpa-specification-vs-springboot-opinionation)

So, the right way is to name your columns strictly in a snake_case, NOT camelCase or PascalCase.

Upvotes: 1

Related Questions