I am new to spring framework. I am trying to create a CRUD application. After running the Spring application, the application starting successfully but database is not getting updated. My database name is Emp. When the springboot application started successfully I refreshed the database but no table named "employees" is added there.
My project structure is as follows:
There is no compilation error as of now.
Following is the generated console after running the application-
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.4)
2023-09-29T18:52:42.309+05:30 INFO 1908 --- [ main] com.cts.Employee.EmployeeApplication : Starting EmployeeApplication using Java 17.0.8 with PID 1908 (C:UsersGalseclipse-workspaceEmployeetargetclasses started by Gals in C:UsersGalseclipse-workspaceEmployee)
2023-09-29T18:52:42.318+05:30 INFO 1908 --- [ main] com.cts.Employee.EmployeeApplication : No active profile set, falling back to 1 default profile: "default"
2023-09-29T18:52:44.171+05:30 INFO 1908 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-09-29T18:52:44.245+05:30 INFO 1908 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 47 ms. Found 0 JPA repository interfaces.
2023-09-29T18:52:45.852+05:30 INFO 1908 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-09-29T18:52:45.880+05:30 INFO 1908 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-09-29T18:52:45.880+05:30 INFO 1908 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.13]
2023-09-29T18:52:46.137+05:30 INFO 1908 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-09-29T18:52:46.140+05:30 INFO 1908 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3654 ms
2023-09-29T18:52:46.617+05:30 INFO 1908 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-09-29T18:52:46.756+05:30 INFO 1908 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.2.9.Final
2023-09-29T18:52:46.762+05:30 INFO 1908 --- [ main] org.hibernate.cfg.Environment : HHH000406: Using bytecode reflection optimizer
2023-09-29T18:52:47.100+05:30 INFO 1908 --- [ main] o.h.b.i.BytecodeProviderInitiator : HHH000021: Bytecode provider name : bytebuddy
2023-09-29T18:52:47.431+05:30 INFO 1908 --- [ main] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer
2023-09-29T18:52:47.468+05:30 INFO 1908 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-09-29T18:52:47.864+05:30 INFO 1908 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@6842c101
2023-09-29T18:52:47.868+05:30 INFO 1908 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2023-09-29T18:52:49.346+05:30 INFO 1908 --- [ main] o.h.b.i.BytecodeProviderInitiator : HHH000021: Bytecode provider name : bytebuddy
2023-09-29T18:52:51.157+05:30 INFO 1908 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-09-29T18:52:51.218+05:30 INFO 1908 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-09-29T18:52:51.389+05:30 WARN 1908 --- [ 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-09-29T18:52:53.771+05:30 INFO 1908 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-09-29T18:52:53.797+05:30 INFO 1908 --- [ main] com.cts.Employee.EmployeeApplication : Started EmployeeApplication in 12.466 seconds (process running for 13.365)
Following is the Application.properties file present in src/main/resources –
spring.datasource.url=jdbc:mysql://localhost:3306/emp?useSSL=false
spring.datasource.username=root
spring.datasource.password=''
# Hibernate properties
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
# create, create-drop
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
** Following is the 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.1.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cts</groupId>
<artifactId>Employee</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Employeemanagement</name>
<description>Employee Management using 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.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<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>
</project>
**Following is my employee model – **
package com.cts.Employee.model;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name="employees")
public class EmployeeModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name="address")
private String address;
}
Please help me resolve the issue.
Tried making changes in application.properties file.
Used
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
instead of
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
but that didn’t help.
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect – this dialect is giving errors in the code. So I had to use spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
2
Answers
Could it be that SpringBoot does not discover your Entity class?
If you have a certain project structure where the main application class is in another path, you might need to add
scanBasePackages
property:Try using
org.hibernate.dialect.MySQL5Dialect
in the Hibernate properties.