0

I'm using Spring Boot 2.4.2 and JPA for generating tables in existing DB. Maven builds project without any issues, and when I'm starting spring boot, there are also no errors, but at the same time no tables are created.

Here is my pom.xml:

<dependencies>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</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>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>

</dependencies>

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

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5433/tiktaktoe

spring.datasource.username=postgres

spring.datasource.password=****

spring.jpa.generate-ddl=true

my classes:

package by.egerag.tiktaktoe.entity;

import javax.persistence.*;
import java.util.List;

@Entity
public class Player {

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

    private String username;

    private String password;

    @ManyToMany(cascade = { CascadeType.ALL })
    @JoinTable(
            name = "role",
            joinColumns = { @JoinColumn(name = "player_id") },
            inverseJoinColumns = { @JoinColumn(name = "role_id") }
    )
    private List<Role> role;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "statistics_id", referencedColumnName = "id")
    private Statistics statistics;

    @OneToMany(mappedBy = "firstPlayer", cascade = CascadeType.ALL)
    private List<Lobby> firstLobby;

    @OneToMany(mappedBy = "secondPlayer", cascade = CascadeType.ALL)
    private List<Lobby> secondLobby;

    public Player() {
    }

    public Player(Integer id, String username, String password, List<Role> role, Statistics statistics) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.role = role;
        this.statistics = statistics;
    }

    public Player(String username, String password, List<Role> role, Statistics statistics) {
        this.username = username;
        this.password = password;
        this.role = role;
        this.statistics = statistics;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Role> getRole() {
        return role;
    }

    public void setRole(List<Role> rights) {
        this.role = rights;
    }

    public Statistics getStatistics() {
        return statistics;
    }

    public void setStatistics(Statistics statistics) {
        this.statistics = statistics;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", rights=" + role +
                ", statistics=" + statistics +
                '}';
    }
}


package by.egerag.tiktaktoe.entity;

import javax.persistence.*;
import java.util.List;

@Entity
public class Role {

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

    private String role;

    @ManyToMany(mappedBy = "role")
    private List<Player> players;

    public Role() {
    }

    public Role(String role) {
        this.role = role;
    }

    public Role(Integer id, String role) {
        this.id = id;
        this.role = role;
    }

    public Role(Integer id, String role, List<Player> players) {
        this.id = id;
        this.role = role;
        this.players = players;
    }

    public Integer getId() {
        return id;
    }

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

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public List<Player> getPlayers() {
        return players;
    }

    public void setPlayers(List<Player> players) {
        this.players = players;
    }

    @Override
    public String toString() {
        return "Role{" +
                "id=" + id +
                ", role='" + role + '\'' +
                ", users=" + players +
                '}';
    }
}


package by.egerag.tiktaktoe.entity;

import javax.persistence.*;

@Entity
public class Statistics {

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

    private Integer win;

    private Integer loose;

    @OneToOne(mappedBy = "statistics")
    private Player player;

    public Statistics() {
    }

    public Statistics(Integer win, Integer loose) {
        this.win = win;
        this.loose = loose;
    }

    public Statistics(Integer id, Integer win, Integer loose) {
        this.id = id;
        this.win = win;
        this.loose = loose;
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getWin() {
        return win;
    }

    public void setWin(Integer win) {
        this.win = win;
    }

    public Integer getLoose() {
        return loose;
    }

    public void setLoose(Integer loose) {
        this.loose = loose;
    }

    @Override
    public String toString() {
        return "Statistics{" +
                "id=" + id +
                ", win=" + win +
                ", loose=" + loose +
                '}';
    }
}



package by.egerag.tiktaktoe.entity;

import javax.persistence.*;

@Entity
public class Lobby {

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

    @ManyToOne
    @JoinColumn(name = "firstPlayer_id", referencedColumnName = "id")
    private Player firstPlayer;

    @ManyToOne
    @JoinColumn(name = "secondPlayer_id", referencedColumnName = "id")
    private Player secondPlayer;

    private Integer turn;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "battlefield_id", referencedColumnName = "id")
    private Battlefield battlefield;

    public Lobby() {
    }

    public Lobby(Player firstPlayer, Player secondPlayer, Integer turn, Battlefield battlefield) {
        this.firstPlayer = firstPlayer;
        this.secondPlayer = secondPlayer;
        this.turn = turn;
        this.battlefield = battlefield;
    }

    public Lobby(Integer id, Player firstPlayer, Player secondPlayer, Integer turn, Battlefield battlefield) {
        this.id = id;
        this.firstPlayer = firstPlayer;
        this.secondPlayer = secondPlayer;
        this.turn = turn;
        this.battlefield = battlefield;
    }

    public Integer getId() {
        return id;
    }

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

    public Player getFirstPlayer() {
        return firstPlayer;
    }

    public void setFirstPlayer(Player firstPlayer) {
        this.firstPlayer = firstPlayer;
    }

    public Player getSecondPlayer() {
        return secondPlayer;
    }

    public void setSecondPlayer(Player secondPlayer) {
        this.secondPlayer = secondPlayer;
    }

    public Integer getTurn() {
        return turn;
    }

    public void setTurn(Integer turn) {
        this.turn = turn;
    }

    public Battlefield getBattlefield() {
        return battlefield;
    }

    public void setBattlefield(Battlefield battlefield) {
        this.battlefield = battlefield;
    }

    @Override
    public String toString() {
        return "Lobby{" +
                "id=" + id +
                ", firstPlayer=" + firstPlayer +
                ", secondPlayer=" + secondPlayer +
                ", turn=" + turn +
                ", battlefield=" + battlefield +
                '}';
    }
}



package by.egerag.tiktaktoe.entity;

import javax.persistence.*;

@Entity
public class Battlefield {

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

    private Integer x1y1;

    private Integer x2y1;

    private Integer x3y1;

    private Integer x1y2;

    private Integer x2y2;

    private Integer x3y2;

    private Integer x1y3;

    private Integer x2y3;

    private Integer x3y3;

    @OneToOne(mappedBy = "battlefield")
    private Lobby lobby;

    public Battlefield() {
    }

    public Battlefield(Integer x1y1, Integer x2y1, Integer x3y1,
                       Integer x1y2, Integer x2y2, Integer x3y2,
                       Integer x1y3, Integer x2y3, Integer x3y3) {
        this.x1y1 = x1y1;
        this.x2y1 = x2y1;
        this.x3y1 = x3y1;
        this.x1y2 = x1y2;
        this.x2y2 = x2y2;
        this.x3y2 = x3y2;
        this.x1y3 = x1y3;
        this.x2y3 = x2y3;
        this.x3y3 = x3y3;
    }

    public Battlefield(Integer id,
                       Integer x1y1, Integer x2y1, Integer x3y1,
                       Integer x1y2, Integer x2y2, Integer x3y2,
                       Integer x1y3, Integer x2y3, Integer x3y3) {
        this.id = id;
        this.x1y1 = x1y1;
        this.x2y1 = x2y1;
        this.x3y1 = x3y1;
        this.x1y2 = x1y2;
        this.x2y2 = x2y2;
        this.x3y2 = x3y2;
        this.x1y3 = x1y3;
        this.x2y3 = x2y3;
        this.x3y3 = x3y3;
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getX1y1() {
        return x1y1;
    }

    public void setX1y1(Integer x1y1) {
        this.x1y1 = x1y1;
    }

    public Integer getX2y1() {
        return x2y1;
    }

    public void setX2y1(Integer x2y1) {
        this.x2y1 = x2y1;
    }

    public Integer getX3y1() {
        return x3y1;
    }

    public void setX3y1(Integer x3y1) {
        this.x3y1 = x3y1;
    }

    public Integer getX1y2() {
        return x1y2;
    }

    public void setX1y2(Integer x1y2) {
        this.x1y2 = x1y2;
    }

    public Integer getX2y2() {
        return x2y2;
    }

    public void setX2y2(Integer x2y2) {
        this.x2y2 = x2y2;
    }

    public Integer getX3y2() {
        return x3y2;
    }

    public void setX3y2(Integer x3y2) {
        this.x3y2 = x3y2;
    }

    public Integer getX1y3() {
        return x1y3;
    }

    public void setX1y3(Integer x1y3) {
        this.x1y3 = x1y3;
    }

    public Integer getX2y3() {
        return x2y3;
    }

    public void setX2y3(Integer x2y3) {
        this.x2y3 = x2y3;
    }

    public Integer getX3y3() {
        return x3y3;
    }

    public void setX3y3(Integer x3y3) {
        this.x3y3 = x3y3;
    }

    public Lobby getLobby() {
        return lobby;
    }

    public void setLobby(Lobby lobby) {
        this.lobby = lobby;
    }

    @Override
    public String toString() {
        return "Battlefield{" +
                "id=" + id +
                ", x1y1=" + x1y1 +
                ", x2y1=" + x2y1 +
                ", x3y1=" + x3y1 +
                ", x1y2=" + x1y2 +
                ", x2y2=" + x2y2 +
                ", x3y2=" + x3y2 +
                ", x1y3=" + x1y3 +
                ", x2y3=" + x2y3 +
                ", x3y3=" + x3y3 +
                '}';
    }
}

and spring boot logs:

2021-02-12 17:25:55.225  INFO 8148 --- [           main] b.egerag.tiktaktoe.TikTakToeApplication  : Starting TikTakToeApplication using Java 11.0.5 on DESKTOP-1RSAF53 with PID 8148 (D:\demo\TikTakToe\target\classes started by HP in D:\demo\TikTakToe)
2021-02-12 17:25:55.257  INFO 8148 --- [           main] b.egerag.tiktaktoe.TikTakToeApplication  : No active profile set, falling back to default profiles: default
2021-02-12 17:25:57.319  INFO 8148 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-02-12 17:25:57.435  INFO 8148 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 108 ms. Found 5 JPA repository interfaces.
2021-02-12 17:25:58.949  INFO 8148 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-02-12 17:25:58.969  INFO 8148 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-02-12 17:25:58.969  INFO 8148 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-02-12 17:25:59.282  INFO 8148 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-02-12 17:25:59.282  INFO 8148 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3732 ms
2021-02-12 17:25:59.537  INFO 8148 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-02-12 17:25:59.784  INFO 8148 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-02-12 17:25:59.864  INFO 8148 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-02-12 17:25:59.975  INFO 8148 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.27.Final
2021-02-12 17:26:00.288  INFO 8148 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-02-12 17:26:00.581  INFO 8148 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2021-02-12 17:26:02.180  INFO 8148 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-02-12 17:26:02.195  INFO 8148 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-02-12 17:26:02.264  WARN 8148 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-02-12 17:26:02.449  INFO 8148 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-02-12 17:26:03.345  INFO 8148 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-02-12 17:26:03.361  INFO 8148 --- [           main] b.egerag.tiktaktoe.TikTakToeApplication  : Started TikTakToeApplication in 9.544 seconds (JVM running for 13.661)

I checked almost all similar questions, but didn't find solution for my case.

1 Answer 1

1

Try to add this to the properties :

spring.jpa.hibernate.ddl-auto = update

Since it defaults to none

Also add this, since you're using postgres :

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, tried both approaches, but result is the same, no tables are created Maybe annotations in classes are incorrect?
I think the annotations are correct. You might also want to try to change it to spring.jpa.hibernate.ddl-auto = create @Egerag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.