Skip to content
Advertisement

Spring Boot cannot create database table in mysql 5.7

I’d like to create a table called dni with that information in mysql server.

@Entity
public class Dni implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, updatable = false)
    private Long id;
    private String nombre;
    private String apellidos;
    private String direccion;
    private String fotoPerfil;
    private String codigoDni;
    private String fechaNacimiento;

    public Dni() {
    }

    public Dni(Long id, String nombre, String apellidos, String direccion, String fotoPerfil, String codigoDni, String fechaNacimiento) {
        this.id = id;
        this.nombre = nombre;
        this.apellidos = apellidos;
        this.direccion = direccion;
        this.fotoPerfil = fotoPerfil;
        this.codigoDni = codigoDni;
        this.fechaNacimiento = fechaNacimiento;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getApellidos() {
        return apellidos;
    }

    public void setApellidos(String apellidos) {
        this.apellidos = apellidos;
    }

    public String getDireccion() {
        return direccion;
    }

    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }

    public String getFotoPerfil() {
        return fotoPerfil;
    }

    public void setFotoPerfil(String fotoPerfil) {
        this.fotoPerfil = fotoPerfil;
    }

    public String getCodigoDni() {
        return codigoDni;
    }

    public void setCodigoDni(String codigoDni) {
        this.codigoDni = codigoDni;
    }

    public String getFechaNacimiento() {
        return fechaNacimiento;
    }

    public void setFechaNacimiento(String fechaNacimiento) {
        this.fechaNacimiento = fechaNacimiento;
    }

    @Override
    public String toString() {
        return "Dni{" +
                "id=" + id +
                ", nombre='" + nombre + ''' +
                ", apellidos='" + apellidos + ''' +
                ", direccion='" + direccion + ''' +
                ", fotoPerfil='" + fotoPerfil + ''' +
                ", codigoDni='" + codigoDni + ''' +
                ", fechaNacimiento=" + fechaNacimiento +
                '}';
    }
}

Querys that failed:

1.”create table dni (id bigint not null, apellidos varchar(255), codigo_dni varchar(255), direccion varchar(255), fecha_nacimiento varchar(255), foto_perfil varchar(255), nombre varchar(255), primary key (id)) engine=MyISAM”

2.”create table hibernate_sequence (next_val bigint) engine=MyISAM”

What I tried:

–> put @Table(name = dni)

–> put this on pom.xml org.hibernate.dialect.MySQL5Dialect

–> put this on application.properties spring.jpa.properties.hibernate.globally_quoted_identifiers=true

Nothing of this worked

Advertisement

Answer

Firstly, When you create just an annotation as @Entity and with sufficient dependency of a database of manual or H2 Database, Spring boot will create a table with the same name as the name of the class.

solution :

Add H2 database dependency for getting the hibernate_sequence table. You will get H2 database dependency in pom.xml as follows :

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

Or

You can add a dependency for MYSQL and connect with the database manually using the application.properties file

pom.xml

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/dnischema
spring.datasource.username=root
spring.datasource.password=root

# Hibernate properties
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# create, create-drop
spring.jpa.hibernate.ddl-auto=update

If u are using the above steps firstly do create a Database in Mysql using the command create database dnischema.

Using this solution u don’t need to create a separate table spring boot will create the table for you.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement