So I have already created my database and all the tables with the appropriate columns in Mysql and connected it to springBoot. My issue is that when I create my user class which matches all the right columns on the Mysql setup, and run the springboot application I get: "Error executing DDL...[Table 'users' already exists]" Here is my application.properties setup
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Hibernate specific properties
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
and my User class
import jakarta.persistence.*;
@Entity
@Table(name = "Users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String firstName;
private String lastName;
private String password;
private Float rating;
// getters and setters
}
and my Users table in sql
CREATE TABLE Users (
User_ID INT AUTO_INCREMENT,
First_Name VARCHAR(100),
Last_Name VARCHAR(100),
Password VARCHAR(255), -- storing hashed password
Rating FLOAT,
PRIMARY KEY (User_ID)
);
Can someone guide me as to why this is happening?
This error occurs because you have set the spring.jpa.hibernate.ddl-auto
property to update, which instructs Hibernate to automatically update the database schema based on the entity classes.
Since the table already exists in the database, Hibernate is trying to execute a DDL (Data Definition Language) statement to create the table again, resulting in the error. To resolve this issue, you have a few options:
Drop the existing table: If you don't have any important data in the "Users"
table, you can drop the table from the database and let Hibernate recreate it. Make sure you have a backup of your data if necessary.
Change spring.jpa.hibernate.ddl-auto to validate
: By changing the value of this property to validate, Hibernate will only validate the entity mapping against the existing database schema. It won't perform any DDL operations or attempt to create the table again.
spring.jpa.hibernate.ddl-auto=validate
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments