Spring Boot、Hibernate、Postgres——不创建表

问题描述 投票:0回答:2

我正在尝试基于实体生成表格。应用程序正确启动,没有创建任何表。怎么了?

我正在使用 postgre sql 版本 14 和 java 17

这是我的pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.quizmenia</groupId>
    <artifactId>quizmeniaserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>quizmeniaserver</name>
    <description>quizmenia - Application to host quiz for students, communities etc</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>

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

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

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
        </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-jdbc</artifactId>
        </dependency>

    </dependencies>

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

</project>

应用程序.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/quizmenia
spring.datasource.username=postgres
spring.datasource.password=admin

spring.jpa.hibernate.ddl-auto=create-drop

spring.datasource.schema=public

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

spring.jpa.open-in-view=true

如您所见,我添加了 spring.jpa.hibernate.ddl-auto=create-drop 行,但 JPA 仍未创建表。怎么了?

用户实体:

package com.quizmenia.quizmeniaserver.model;

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

import com.fasterxml.jackson.annotation.JsonIgnore;


@Entity
@Table(name = "users")
public class user {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false)
    private Long userId;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    @Column(name="email")
    private String email;

    @Column(name="password")
    private String password;

    @Column(name="enable")
    private Boolean enable;

    @Column(name="about")
    private String about;

    @Column(name="phone_number")
    private Integer phoneNumber;


    public user() {
        
    }



    public user(Long userId, String firstName, String lastName, String email, String password, Boolean enable,
            String about, Integer phoneNumber) {
        this.userId = userId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.enable = enable;
        this.about = about;
        this.phoneNumber = phoneNumber;
    }


    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

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

    public Boolean getenable() {
        return enable;
    }

    public void setenable(Boolean enable) {
        this.enable = enable;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public Integer getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(Integer phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

}

文件夹结构:

java spring spring-boot hibernate maven
2个回答
0
投票

我认为 application.properties 文件有问题 您可以在What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

find some useful things about hbm2ddl
spring.datasource.url=jdbc:postgresql://localhost:5432/quizmenia
spring.datasource.username=postgres
spring.datasource.password=admin
spring.jpa.hibernate.ddl-auto=create
spring.datasource.schema=public
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true  
spring.jpa.properties.hibernate.dialect=..
spring.jpa.open-in-view=true

注意:当 show SQL 为真时,首先检查控制台中显示的查询。


0
投票

尝试改变:

@Table(name = "users")

致:

@Table(name = "users",  schema = "public", catalog = "quizmenia")
© www.soinside.com 2019 - 2024. All rights reserved.