Home > Enterprise >  MySQL and Hibernate are not configuring
MySQL and Hibernate are not configuring

Time:01-12

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);
    }

}

CodePudding user response:

 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

CodePudding user response:

-- 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
  • Related