尝试在Spring Boot中创建用户类时出现表已存在错误

huangapple go评论71阅读模式
英文:

Table already exists error when trying to create user class in springboot

问题

我已经创建了我的数据库以及所有在MySQL中具有适当列的表,并将其连接到Spring Boot。我的问题是,当我创建与MySQL设置中的所有正确列匹配的用户类并运行Spring Boot应用程序时,我收到以下错误消息:"Error executing DDL...[Table 'users' already exists]"。以下是我的application.properties设置:

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

以及我的用户类:

package com.spasli.mvp.model;

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
}

以及我的SQL中的Users表:

CREATE TABLE Users (
    User_ID INT AUTO_INCREMENT,
    First_Name VARCHAR(100),
    Last_Name VARCHAR(100),
    Password VARCHAR(255), -- 存储哈希密码
    Rating FLOAT,
    PRIMARY KEY (User_ID)
);

有人可以指导我为什么会发生这种情况吗?

英文:

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?

答案1

得分: 1

这个错误是因为你将 spring.jpa.hibernate.ddl-auto 属性设置为 update,这会指示Hibernate根据实体类自动更新数据库架构。

由于表格已经存在于数据库中,Hibernate尝试执行DDL(数据定义语言)语句以再次创建表格,从而导致错误。要解决此问题,你有几个选项:

  1. 删除现有表格:如果你在 "Users" 表格中没有重要数据,你可以从数据库中删除表格,然后让Hibernate重新创建它。如果需要的话,请确保备份数据。

  2. spring.jpa.hibernate.ddl-auto 更改为 validate:通过将此属性的值更改为 validate,Hibernate将只验证实体映射与现有数据库架构的匹配性。它不会执行任何DDL操作或尝试再次创建表格。

    spring.jpa.hibernate.ddl-auto=validate

英文:

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:

  1. 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.

  2. 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

答案2

得分: 0

我猜问题出在你的列名上。你已经设置了 spring.jpa.hibernate.ddl-auto=update,这意味着当Spring Boot项目运行时,它会查看数据库,如果表不存在,它会创建新表,如果表存在,它会根据需要进行更新。但Spring JPA也会查找列名、列类型、列长度和其他属性。因此,如果它们不匹配,Spring会认为表不存在。因此,它将尝试创建新表。但名为 Users 的表是存在的。在这种情况下,Spring 将抛出 Table is already exists。要解决这个问题,你可以:

  1. 从数据库中删除表,并重新运行Spring Boot项目。让Spring项目自动生成相同的表结构。这样表就与你的实体相同,问题就解决了。
  2. spring.jpa.hibernate.ddl-auto 属性更改为 spring.jpa.hibernate.ddl-auto=create。这样Spring Boot将在数据库中删除表,并在每次运行时创建新表。这在开发阶段很有用。但在每次运行时表都会被删除和重新创建,因此数据会丢失。在切换到此属性时,你必须三思而行。
  3. spring.jpa.hibernate.ddl-auto 属性更改为 spring.jpa.hibernate.ddl-auto=none。它将不做任何事。因此,Spring将使用数据库中的表,并且不会更改任何内容。但如果列名或类型不同,它将抛出另一个问题。
  4. 更改实体的列名或表的列名。使列名、类型、长度等相同。如果你不想更改数据库中的表,你可以更改实体。你可以完全更改变量名,或者你可以使用 @Column 注解。对于你的实体的示例:
import jakarta.persistence.*;
@Entity
@Table(name = "Users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "User_ID")
    private Integer id;

    @Column(name = "First_Name")
    private String firstName;

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

    @Column(name = "Password")
    private String password;

    @Column(name = "Rating")
    private Float rating;

    // getters and setters
}

我希望这能帮到你。

英文:

I guess problem is your column names. You have set your spring.jpa.hibernate.ddl-auto=update, so that means, when spring boot project runs, it will look at datbase, if table is not exists, it will create new table, if table exists, it will update if necessary. But spring jpa also look up for column names, column types and column lengths and other properties. So if they don't match, spring thinks that table is not exist. So it will try to create new table. But table with Users name is exist. In this situation, Spring will throw Table is already exists. To fix this problem, you can:

  1. Delete table from database, and run spring boot project again. Let spring project to generate table itself. So table will be same with your entity, problem will be solved.
  2. Change spring.jpa.hibernate.ddl-auto property to spring.jpa.hibernate.ddl-auto=create. So spring boot will drop table in database and will create new one at each running. It is useful at development time. But at each running table will be dropped and re created, so your data will be lost. You have to think twice when switching to this property.
  3. Change spring.jpa.hibernate.ddl-auto property to spring.jpa.hibernate.ddl-auto=none. It will be do nothing. So Spring will use table in database and will not change anything. But if column names or types is different, it will throw another problem.
  4. Change colum names of entity, or table. Make same column names, types, length etc. If you don't want to change your table in databse, you can change your entity. You can change exactly variable names, or you can use @Column annotation. Example for your entity:
import jakarta.persistence.*;
@Entity
@Table(name = "Users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "User_ID")
    private Integer id;

    @Column(name = "First_Name")
    private String firstName;

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

    @Column(name = "Password")
    private String password;

    @Column(name = "Rating")
    private Float rating;

    // getters and setters
}

I hope this will help you.

答案3

得分: 0

如果您确定表结构,请在application.properties文件中设置:

spring.jpa.hibernate.ddl-auto=none

但在开发过程中,您可能需要更改一些实体及其属性,因此最好设置为:

spring.jpa.hibernate.ddl-auto=update

从现在开始,任何更改都将反映在数据库中的表中。

英文:

If you are sure about tables structures set

spring.jpa.hibernate.ddl-auto=none

in application.properties file.
But during development you will need to change some entities and their properties so it is better set

spring.jpa.hibernate.ddl-auto=update

From now any changes will be reflected on your tables in database.

答案4

得分: 0

spring.jpa.hibernate.ddl-auto=validate设置在你的application.properties文件中。
它会检查你的MySQL数据库是否与你的实体相同,如果有问题,你的应用程序将无法运行。

英文:

set spring.jpa.hibernate.ddl-auto=validate in your application.properties.
it will check that your mysql db is same as your entity and if there is a problem your app will not run .

huangapple
  • 本文由 发表于 2023年6月2日 11:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定