Gibran Mohammad Khan
Gibran Mohammad Khan

Reputation: 85

Hibernate JPA Spring Boot

I have created all the models for my spring boot application and successfully specified the relation between them as per business logic. In my properties file, I set the hibernate auto-ddl option as create to generate all the tables as per the relationship between the entities. I am also using liquibase for database migration.

The problem is that, from the logs I can see that hibernate is altering the table before creating it and therefore throwing the run time exception saying "Table not found". Why it is altering the table before creating it? How to solve this issue?

Any help would be highly appreciable. Thanks in advance.

Some logs example

Error executing DDL "alter table application drop foreign key FKrtuepaxepo3o6x0pkn0w62ucg" via JDBC Statement.

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "APPLICATION" not found; SQL statement:
alter table application drop foreign key FKrtuepaxepo3o6x0pkn0w62ucg

.......
.......
.......
Hibernate: drop table if exists application
create table application (id bigint not null auto_increment
alter table application add constraint UK_5jl5nuoh207t0japuutb4avd4 unique (application_name)

Why it is expecting table name as "APPLICATION" before creating "application"

@Table(name = "application")
    @EqualsAndHashCode(onlyExplicitlyIncluded = true)
    public class Application extends AbstractAuditingEntity implements Serializable {

        private static final long serialVersionUID = 1L;
}

I am using H2 database in the development and MYSQL in production. But, previously, I was using the "org.hibernate.dialect.MySQL5InnoDBDialect" dialect in "application-dev.yml". Changing it to h2 dialect(org.hibernate.dialect.H2Dialect) solved the problem.

But,what should be the proper dialect in application-prod.yml? I am using MYSQL in production. Current dialect I am using here is "org.hibernate.dialect.MySQL5InnoDBDialect". This will further create problem in production.

application-prod.yml

datasource:
#    type: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/appscoredb
    username:
    password:
    hikari:
      poolName: Hikari
      auto-commit: false
#  h2:
#    console:
#        enabled: true
#        settings:
#          web-allow-others: true
  jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    database: mysql
    hibernate:
      ddl-auto: none

    show-sql: true
    properties:
      hibernate.id.new_generator_mappings: true
      hibernate.connection.provider_disables_autocommit: true
      hibernate.cache.use_second_level_cache: false
      hibernate.cache.use_query_cache: false
      hibernate.generate_statistics: false
  liquibase:
    # Remove 'faker' if you do not want the sample data to be loaded automatically
    contexts: prod, faker
    enabled: false

Upvotes: 0

Views: 289

Answers (1)

Gibran Mohammad Khan
Gibran Mohammad Khan

Reputation: 85

It is solved by using this dialect: org.hibernate.dialect.MySQL5Dialect

I hope it could help someone in near future.

Upvotes: 1

Related Questions