我无法使用springboot在数据库postgresql中创建表

时间:2020-01-14 15:19:12

标签: postgresql hibernate spring-boot

我正在尝试springboot在PostgreSQL数据库中创建一个表,spring可以正确连接到数据库,但是不创建任何表。

pgadmin display

休眠配置

application.yaml

spring:

  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: 1234
    driver-class-name: org.postgresql.Driver

  jpa:
    hibernate:
      ddl-auto: create-drop
    generate-ddl: true
    show-sql: true  

这是我需要放入数据库中的实体

User.java

@Data
@Entity
@Table(name= "users")
public class User {
    //iduser
    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy="uuid2")
    private String idUser;

    private String firstName;
    private String lastName;
    private String email;
    private String password;
    private String rol;
    private String language;

    public User(){

    }
}

PostgressApplication.Java

package com.sample.postgres;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages="com.sample")
public class PostgresApplication {

    public static void main(String[] args) {
        SpringApplication.run(PostgresApplication.class, args);

    }

}

pom.xml

我的项目中有很多依赖项,您怎么看

<?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 http://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>2.1.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sample</groupId>
    <artifactId>postgres</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>postgres</name>
    <description>Demo project for Spring Boot</description>

    <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>

            <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--  JPA y Postgres -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>

    </dependencies>

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

</project>

My estructure code

3 个答案:

答案 0 :(得分:0)

创建-删除策略将在您的应用程序关闭后删除。请尝试创建

其次:查看您的属性文件:

spring:

datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: 1234
    driver-class-name: org.postgresql.Driver

  jpa:
    hibernate:
      ddl-auto: create-drop
    generate-ddl: true
    show-sql: true  

以某种方式缩进,看起来好像找不到您的jpa配置,因为它嵌套在数据源下面。

尝试:

spring:

  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: 1234
    driver-class-name: org.postgresql.Driver

  jpa:
    hibernate:
      ddl-auto: create-drop
    generate-ddl: true
    show-sql: true
    database-platform: org.hibernate.dialect.PostgreSQL9Dialect
      properties:
        hibernate:
          temp:
            use_jdbc_metadata_defaults: false
          jdbc:
            lob:
              non_contextual_creation: true

答案 1 :(得分:0)

尝试仅使用create而不是create-drop

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: 1234
    driver-class-name: org.postgresql.Driver
  jpa:
    hibernate:
      ddl-auto: create
    generate-ddl: true
    show-sql: true
    database-platform: org.hibernate.dialect.PostgreSQL9Dialect
      properties:
        hibernate:
          temp:
            use_jdbc_metadata_defaults: false
          jdbc:
            lob:
              non_contextual_creation: true

答案 2 :(得分:0)

简短答案:在@EntityScan("com.sample.modelo")类的顶部添加PostgresApplication

package com.sample.postgres;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages="com.sample")
@EntityScan("com.sample.modelo")
public class PostgresApplication {

    public static void main(String[] args) {
        SpringApplication.run(PostgresApplication.class, args);

    }

}

更多信息:

您的配置对我来说似乎很好。确保已扫描您的实体。我的意思是您可能已经以不读取您的实体类的方式配置了项目。这是您需要具备的最低配置:

  1. 使用此依赖项
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
  1. application.properties放在src/main/resources
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=mysecretpassword

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

  1. 创建您的实体(您已经拥有此实体)
@Data
@Entity
@Table(name= "users")
public class User {
    //iduser
    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy="uuid2")
    private String idUser;

    private String firstName;
    private String lastName;
    private String email;
    private String password;
    private String rol;
    private String language;

    public User(){

    }
}
  1. 参加@SpringBootApplication课程
package your.package;

@SpringBootApplication
public class MainApp {

    public static void main(String[] args) {
        SpringApplication.run(StackTest.class);
    }

}

这比这里重要!如果您的实体类User不在your.package的子包中,那么您需要向@SpringBootApplication类添加自定义注释。

@SpringBootApplication
@EntityScan("my.other.package")
public class MainApp {

}

有关Hibernate配置的更多信息

在不使用Spring-boot的情况下使用JPA配置应用程序时,您需要提供实体类的位置(以及许多其他信息)。

Configuration configuration = new Configuration();
// many codes here
configuration.addPackage("my.package.for.entities");

这告诉hibernate扫描特定的软件包以查找您的实体。

使用spring-boot,您不必编写整个配置类。但是,如果您的实体(这是您的情况)与@SpringBootApplication位于不同的包中,那么您需要指定要使用注释@EntityScan("my.package.for.entities")进行扫描的包。确实,这个注释由类JpaBaseConfiguration解释以查找您的实体。

注释@SpringBootApplication(scanBasePackages = "my.package")在这里用于查找您的Spring bean,而不是您的实体。

如果一切正常,您应该在日志中看到它:

休眠:如果存在用户级联,则删除表 休眠状态:创建表用户(id_user varchar(255)不为null,电子邮件varchar(255),first_name varchar(255),语言varchar(255),last_name varchar(255),密码varchar(255),rol varchar(255),主键(id_user))

让我知道。