Hibernate does not create table automatically in mysql

Asked

Viewed 1,921 times

2

I’m studying Spring with a little hibernate, where I stopped in a situation I can’t pass.

I configured the code with Hibernate for creating tables in the database but when I run the application it does not create.

My model class:

@Entity

@Table(name="cliente")

public class Cliente {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String nome;


    public int getId() {
        return id;
    }

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

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }
}

My properties:

server.port=${port:8080}
spring.datasource.url=jdbc:mysql://localhost:3306/clientesdb
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.jpa.hibernate.ddl-auto = update

Mysql without password.

I’m doing something wrong?

  • I usually put create=true; in the url, see if it works this way: spring.datasource.url=jdbc:mysql://localhost:3306/clientesdb;create=true besides you need to define the dialect spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

4 answers

2


I decided to put the annotation to scan my entity package in my Application class:

@Entityscan(basePackages = {"br.com.springboot.model"})

1

I solved my problem by making the correct import(javax.persistence.Entity) I am using spring.jpa.Hibernate.ddl-auto=update

  • Please add more details to expand your response, such as a working code or documentation quotes.

1

I was making that same mistake.

I managed to solve using:spring.jpa.hibernate.ddl-auto=update. Remember that I had put ?useTimezone=true&serverTimezone=UTC in my datasource.url, but I think this served only for another problem I had before. My properties got this way:

spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:mysql://localhost:3306/nome_do_database?useTimezone=true&serverTimezone=UTC spring.datasource.username=usuario spring.datasource.password=senha spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

1

Missing add the dialect in their properties

spring.datasource.url=jdbc:mysql://localhost:3306/clientesdb​;create=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true

I also put the show-sql if not certain, post the sql being generated

Another important point is also to check the import of your Entity, check if your import is javax.persistence.Entity

  • Hello, I solved the problem by putting in my Application class the annotation: @Entityscan(basePackages = {"br.com.springboot.model"}) As if I had forced myself to find the entity package. This import that you mentioned "javax.persistence.Entity" it serves for what?

  • @Philippenunes What did he mean about the javax.persistence.Entity was: https://answall.com/a/168101/132

  • @Philippe Nunes Generally spring itself makes the mapping of the basePackages when you use @SprintBootApplication or @ComponentScan, is probably some kind of configuration that did not let it map, in the latest versions of jpa you are required to set the entity within the EntityScan XML, but with spring this is not necessary. Thanks @Victor Stafusa, that’s right, I believe he may have imported the @Entity another package, this way spring could not map

Browser other questions tagged

You are not signed in. Login or sign up in order to post.