Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot JPA和Spring引导不生成DB表_Spring Boot_Jpa - Fatal编程技术网

Spring boot JPA和Spring引导不生成DB表

Spring boot JPA和Spring引导不生成DB表,spring-boot,jpa,Spring Boot,Jpa,我使用SpringBoot2.4.2和JPA在现有数据库中生成表。Maven构建项目时没有任何问题,当我启动SpringBoot时,也没有错误,但同时没有创建表 以下是我的pom.xml: <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-

我使用SpringBoot2.4.2和JPA在现有数据库中生成表。Maven构建项目时没有任何问题,当我启动SpringBoot时,也没有错误,但同时没有创建表

以下是我的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>
我的班级:

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 +
                '}';
    }
}

我检查了几乎所有类似的问题,但没有找到解决方案。

尝试将此添加到属性中:

spring.jpa.hibernate.ddl-auto = update
因为它默认为
none

另外,由于您使用的是postgres,请添加以下内容:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

您好,两种方法都试过了,但结果是一样的,没有创建表。可能类中的注释不正确?我认为注释是正确的。您可能还想尝试将其更改为
spring.jpa.hibernate.ddl-auto=create
@Egerag
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect