Jpa Hibernate does not create table in H2

Asked

Viewed 930 times

0

Good night,

I am new in Java development - Spring Boot and following a tutorial I can not generate the table in the H2 database, already tried to make some changes in the application.propreties but without success.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-Starter-Parent 2.0.0.RELEASE com.julio.curswto curswto 0.0.1-SNAPSHOT curswto Java home study for conceptual modeling course with UML

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Estrutura do projeto

1 answer

4


You are using wrong properties, it should be:

spring.jpa.hibernate.ddl-auto=update

As described in Ocds:

spring.jpa.hibernate.ddl-auto= # DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Defaults to "create-drop" when using an embedded database and no schema manager was detected. Otherwise, defaults to "none". 

You can check the correct properties on documentation


Your package structure is incorrect, please follow the structure described in documentation.

In short:

  • Component Scan is the Spring feature that scans your classes and automatically configures entities and service classes, repositories, etc...

This is automatically performed for all packages that are below the hierarchy of their class main, in your case com.example.demo.

You have classes outside this hierarchy (such as curso.julio.cursomc.domain. These classes shall not be scanned.

Change the hierarchy of your packages and should work accordingly.

  • Thanks for the information, however even after the correction the table was not created in the bank.

  • Would it be possible to make your project structure available? Your entities are in the hierarchy of the Spring Boot Scan?

  • At this point in the tutorial I am following, nothing has been explained about Component Scan, at the moment when the instructor creates the tables in the database, there are only 3 classes. The codes are attached.

  • @JULIOCESARSILVADEFREITAS edited the answer with new information

Browser other questions tagged

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