Springboot does not generate the tables in the bank

Asked

Viewed 142 times

0

When I run the project, my tables do not appear in the database. I noticed that when I take my relations out of the attributes that I declared @Onetomany, it creates the tables, but my project needs those relationships and the other classes are related right by the project. When I put and run the project, it doesn’t create again, is it some spring bug? Obs: no error appears in the log and the annotations are all right.

My class

@NoArgsConstructor 
@AllArgsConstructor 
@Data @Entity(name = "tb_user") 
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 75, nullable = false)
    private String name;

    @Column(length = 75, nullable = false, unique = true)
    private String email;

    @Column(length = 100, nullable = false)
    private String password;

    @Column(length = 40, nullable = false)
    @Enumerated(EnumType.STRING)
    private Role role;

    @OneToMany(mappedBy = "user")
    private List<Request> requests = new ArrayList<Request>();

    @OneToMany(mappedBy = "user")
    private List<RequestStage> requestStages = new ArrayList<RequestStage>(); }

My dependencies

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

My application.properties

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/db_springteste?useTimezone=true&serverTimezone=UTC
spring.datasource.username=ddsinfo
spring.datasource.password=dds21231

1 answer

1

Its configuration is not correct. According to documentation, if you want the tables to be created, you must use the option create:

spring.jpa.hibernate.ddl-auto=create

A point of attention, when restarting the application, the basis will be recreated, normally this type of option should not be used in production.

The production recommendation would be to use validate to validate only whether the data model used in the application is compatible with the data model present in the database.

Browser other questions tagged

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