Home > database >  Hibernate not getting invoked to be able to create Table in MySQL8
Hibernate not getting invoked to be able to create Table in MySQL8

Time:01-04

I am writing a simple Spring Boot program to create Table in MySQL8: Hibernate is not invoked to create the Table. Kindly check below details:

package com.**************.spring.data.jpa.tutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringDataJpaTutorialApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringDataJpaTutorialApplication.class, args);
   }

}

package com.*************.spring.data.jpa.tutorial.entity;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Student {

    @Id
    private long studentId;
    private String firstName;
    private String lastName;
    private String emailId;
    private String guardianName;
    private String guardianEmail;
    private String guardianMobile;

}

Application.properties :

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/schooldb
spring.datasource.username=root
spring.datasource.password=Maserati@217
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>3.0.1</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.dailycodebuffer</groupId>
   <artifactId>spring-data-jpa-tutorial</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>spring-data-jpa-tutorial</name>
   <description>Demo project for Spring Boot</description>
   <properties>
      <java.version>17</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>javax.persistence</groupId>
         <artifactId>javax.persistence-api</artifactId>
         <version>2.2</version>
      </dependency>
      <dependency>
         <groupId>javax.annotation</groupId>
         <artifactId>javax.annotation-api</artifactId>
         <version>1.3.2</version>
      </dependency>
      <dependency>
         <groupId>com.mysql</groupId>
         <artifactId>mysql-connector-j</artifactId>
         <version>8.0.31</version>
      </dependency>
      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
               <excludes>
                  <exclude>
                     <groupId>org.projectlombok</groupId>
                     <artifactId>lombok</artifactId>
                  </exclude>
               </excludes>
            </configuration>
         </plugin>
      </plugins>
   </build>

</project>

Once I execute the application, Tomcat server starts but Hibernate is not invoked:

2023-01-03T21:27:53.215 05:30  INFO 27612 --- [           main] s.d.j.t.SpringDataJpaTutorialApplication : Starting SpringDataJpaTutorialApplication using Java 18 with PID 27612 (C:\Users\I322706\Downloads\spring-data-jpa-tutorial\target\classes started by I322706 in C:\Users\I322706\Downloads\spring-data-jpa-tutorial)
2023-01-03T21:27:53.218 05:30  INFO 27612 --- [           main] s.d.j.t.SpringDataJpaTutorialApplication : No active profile set, falling back to 1 default profile: "default"
2023-01-03T21:27:54.077 05:30  INFO 27612 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-01-03T21:27:54.115 05:30  INFO 27612 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17 ms. Found 0 JPA repository interfaces.
2023-01-03T21:27:54.872 05:30  INFO 27612 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-01-03T21:27:54.887 05:30  INFO 27612 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-01-03T21:27:54.887 05:30  INFO 27612 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.4]
2023-01-03T21:27:55.065 05:30  INFO 27612 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-01-03T21:27:55.074 05:30  INFO 27612 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1771 ms
2023-01-03T21:27:55.328 05:30  INFO 27612 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-01-03T21:27:55.393 05:30  INFO 27612 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.1.6.Final
2023-01-03T21:27:55.566 05:30  WARN 27612 --- [           main] org.hibernate.orm.deprecation            : HHH90000021: Encountered deprecated setting [javax.persistence.sharedCache.mode], use [jakarta.persistence.sharedCache.mode] instead
2023-01-03T21:27:55.692 05:30  INFO 27612 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-01-03T21:27:56.010 05:30  INFO 27612 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@24a4e2c5
2023-01-03T21:27:56.010 05:30  INFO 27612 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-01-03T21:27:56.026 05:30  INFO 27612 --- [           main] SQL dialect                              : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2023-01-03T21:27:56.026 05:30  WARN 27612 --- [           main] org.hibernate.orm.deprecation            : HHH90000026: MySQL8Dialect has been deprecated; use org.hibernate.dialect.MySQLDialect instead
2023-01-03T21:27:56.490 05:30  INFO 27612 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-01-03T21:27:56.525 05:30  INFO 27612 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-01-03T21:27:56.648 05:30  WARN 27612 --- [           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
2023-01-03T21:27:57.267 05:30  INFO 27612 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-01-03T21:27:57.282 05:30  INFO 27612 --- [           main] s.d.j.t.SpringDataJpaTutorialApplication : Started SpringDataJpaTutorialApplication in 4.671 seconds (process running for 5.328)

It shows the application is started but table is not getting created.

CodePudding user response:

There is no create option for Hibernate.

Values are: none, validate, update, create-drop

So I assume that you want to use update:

spring.jpa.hibernate.ddl-auto=update

CodePudding user response:

I got the same error before , but the problem with hibernate is can do all the staff of ORM process but this can be done only if the table you working on is already created ( only the database not the tables ) , so try create the same database with the same name declared in properties file and check if the problem persist .

  • Related