skip to Main Content

I’m working on a spring boot project example and I have already created the database but when I configure the database, it doesn’t seem to work. It doesn’t give me an error though but when I execute the application, nothing shows on the console that I have added the hibernate properties.
I was expecting something like HHH000204:Processing PersistenceUnitInfo etc.
As a result of that, when I tried creating columns in table for the Employee entity it didn’t work.

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.kessie</groupId>
    <artifactId>Springboot-backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Springboot-backend</name>
    <description>Springboot Restful Web Services</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </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>

application.properties

spring.datasource.url=jdbc:mysql//localhost:3306/employee_management_system?useSSL=false

spring.datasource.username=root
spring.datasource.password=root

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.hibernate.ddl-auto=update

Employee.java

package com.kessie.model;

import jakarta.persistence.*;
import lombok.Data;

@Data
@Entity
@Table(name = "Employee")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private long id;

    @Column(name = "first_name", nullable = false)
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

}

SptingbootApplication.java with Main class

package com.kessie;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;


@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@SpringBootApplication
public class SpringbootBackendApplication {

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

}

Console after execution

2023-01-12T01:36:34.051-08:00  INFO 3832 --- [           main] c.kessie.SpringbootBackendApplication    : Starting SpringbootBackendApplication using Java 19.0.1 with PID 3832 (C:UsersKoranteng K. KessieDocumentsSpring bootProjectsSpringboot backendSpringboot-backendtargetclasses started by Koranteng K. Kessie in C:UsersKoranteng K. KessieDocumentsSpring bootProjectsSpringboot backend)
2023-01-12T01:36:34.058-08:00  INFO 3832 --- [           main] c.kessie.SpringbootBackendApplication    : No active profile set, falling back to 1 default profile: "default"
2023-01-12T01:36:35.390-08:00  INFO 3832 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-01-12T01:36:35.400-08:00  INFO 3832 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-01-12T01:36:35.400-08:00  INFO 3832 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.4]
2023-01-12T01:36:35.519-08:00  INFO 3832 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-01-12T01:36:35.522-08:00  INFO 3832 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1365 ms
2023-01-12T01:36:36.067-08:00  INFO 3832 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-01-12T01:36:36.078-08:00  INFO 3832 --- [           main] c.kessie.SpringbootBackendApplication    : Started SpringbootBackendApplication in 2.574 seconds (process running for 3.094)

3

Answers


  1.  spring.jpa.show-sql=true
    
     spring.jpa.properties.hibernate.format_sql=true
    
     spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    

    Add these lines of code to you application.properties it will print sql queries in formated form and let me know if it works or not

    Login or Signup to reply.
  2. — change the MySQL dependency in pom.xml In ur MySQL dependency artifactId is MySQL-connector-j it’s not fulfilled Add Datasource driver in ur application properties like check the below answer —

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency> 
    
    -- application properties-- 
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver   
    spring.datasource.url=jdbc:mysql://localhost:3306/employee_management_system
    spring.datasource.username=root  
    spring.datasource.password=root
    spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
    spring.jpa.generate-ddl=true
    spring.jpa.hibernate.ddl-auto = update
    
    Login or Signup to reply.
  3. The problem is your code, dependencies and configuration.

    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
    

    This does exactly what it is meant to do, disable the configuration of a DataSource. If you have no DataSource there is no JPA as that will need a DataSource to connect to the database.

    Remove the aforementioned line from your class.

    There is another problem in your code, or actual your dependencies.

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      <scope>provided</scope>
    </dependency>
    

    The scope provided means I use this dependency but yuo need not to include it as it is already part of the server/runtime I’m using. Which means it isn’t on the classpath so no JPA dependencies are found and thus no JPA auto configuration will happen.

    Remove the <scope> tag and it will be included.

    Next your configuration is wrong. You have an illegal JDBC url in there as well as a non-existing Hibernate Dialect (it got removed in Hibernate 6).

    spring.datasource.url=jdbc:mysql//localhost:3306/employee_management_system?useSSL=false
    

    You have a missing : after mysql which leads to no JDBC driver being detected to handle the URL, leading to no DataSource being configured.

    spring.datasource.url=jdbc:mysql://localhost:3306/employee_management_system?useSSL=false
    

    Last, but not least, you also have a misconfiguration of the Hibernate dialect. The one you have doesn’t exist anymore in Hibernate 6.

    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
    

    The org.hibernate.dialect.MySQL5InnoDBDialect got removed in Hibernate 6. Here is the list of build-in dialects for Hibernate 6.1. You should be using org.hibernate.dialect.MySQLDialect as the other ones are deprecated and will eventually be removed as well.

    TIP: instead of using spring.jpa.properties.hibernate.dialect you can use spring.jpa.database-platform to set the dialect.

    spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
    

    NOTE: You probably had this issue before and thus leading you down the path to exclude the JPA stuff (the <scope>provided</scope> and disabling auto config for the DataSource). This made the error go away but didn’t fix it as it simply disabled stuff.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search