0

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.

  • By "create Table" do you mean to literally create a table in your MySQL, `schooldb` database? Or do you mean that Hibernate is not validating that a table `student` exists in `schooldb`? – Jetto Martínez Jan 03 '23 at 16:31
  • Yes I want to create Table in MySQL, schooldb database. – Manish Verma Jan 03 '23 at 16:32
  • If you look at the runtime logs, Hibernate is not invoked to create the table in MySQL – Manish Verma Jan 03 '23 at 16:33
  • Control is just sitting there as if an infinite loop got struck: – Manish Verma Jan 03 '23 at 16:34
  • Ok, I'd need to research to be 100% sure, but I **believe** Hibernate does not create the tables by default. I know big projects, like `SonarQube` that create their own database on deployment, but it's an explicit action. That is: they actually went and programmed that (At least in older versions, like 7.8). The automatic build feature you look for is available in in-memory databases like H2 (I think SQLite also supports it in SpringBoot). Hopefully, I'm wrong to make things easier for you, but I'd keep that in mind. – Jetto Martínez Jan 03 '23 at 16:42
  • This is the page on the [SonarQube product stating that a database must be created before starting the server](https://docs.sonarqube.org/8.9/setup-and-upgrade/install-the-server/), meaning it's one way to do what you'd need. (Check the 'Installing the Database' section). – Jetto Martínez Jan 03 '23 at 16:43
  • Database is already created with the name "schooldb". I am trying to create Table inside that database. – Manish Verma Jan 03 '23 at 16:50
  • Please, check the solutions here: https://stackoverflow.com/questions/26881739/unable-to-get-spring-boot-to-automatically-create-database-schema – Jetto Martínez Jan 03 '23 at 17:10
  • Not working for my case. – Manish Verma Jan 03 '23 at 17:57

3 Answers3

0

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
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
0

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 .

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 03 '23 at 19:40
0

Instead of Javax.persistence, I used Jakarta.persistence and Spring Boot 3.0 application created Table in MySQL. I believe Javax has been deprecated from Spring Boot 3.0 onwards. Kindly correct me if I am wrong.