Search code examples
javamysqlspringspring-boothibernate

Springboot not creating database tables


i created my entity in springboot

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Table;

@Table(name="users") 
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String email;
}

i setted the database configuration in application.properties (i'm using MySQL XAMPP Apache server)

spring.datasource.url=jdbc:mysql://localhost:3307/mediatech?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
server.port=8082

when i run the application it creates the database but not the tables, how can i fix it and create the database schema tables ?


Solution

  • The problem is on the value of spring.jpa.hibernate.ddl-auto should be update.

    the difference between create and update:

    • "create": Hibernate will create the database schema from scratch on every application startup.

    • "update": Hibernate will attempt to update the database schema to match the entity model. It will add tables, columns, and constraints as needed, but it won't drop existing tables or columns. This is a safer option for development and is sometimes used in production.