我可以´;t使用springboot在我的数据库postgresql中创建表

我可以´;t使用springboot在我的数据库postgresql中创建表,postgresql,hibernate,spring-boot,Postgresql,Hibernate,Spring Boot,我试图让springboot在我的PostgreSQL数据库中创建一个表,spring正确地连接到数据库,但不创建任何表 休眠配置 应用程序.yaml spring: datasource: url: jdbc:postgresql://localhost:5432/postgres username: postgres password: 1234 driver-class-name: org.postgresql.Driver jpa:

我试图让springboot在我的PostgreSQL数据库中创建一个表,spring正确地连接到数据库,但不创建任何表

休眠配置

应用程序.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(){

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

4.0.0
org.springframework.boot

应用程序关闭后,创建删除策略将被删除。请尝试创建

旁边:查看您的属性文件:

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

简短回答:在应用程序类的顶部添加
@EntityScan(“com.sample.modelo”)

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

    }

}
更多信息:

我觉得你的配置很好。确保您的实体已扫描。我的意思是,您可能以不读取实体类的方式配置了项目。以下是您需要具备的最低配置:

  • 使用此依赖项
  • 创建您的实体(您已经拥有此实体)
  • 让您的
    @springbootplication
    课程
  • 这才是最重要的!如果您的实体类
    User
    不在.package
    的子包中,那么您需要向
    @springbootplication
    类添加自定义注释

    @SpringBootApplication
    @EntityScan("my.other.package")
    public class MainApp {
    
    }
    
    更多关于Hibernate配置的信息 在使用JPA配置应用程序时,如果不使用Spring boot,则需要提供实体类的位置(以及更多其他信息)

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

    使用SpringBoot,您不必编写整个配置类。但是如果您的实体(这是您的情况)与
    @springboot应用程序
    位于不同的包中,那么您需要使用注释
    @EntityScan(“my.package.for.entities”)
    指定要扫描的包。实际上,这个注释由类
    JpaBaseConfiguration
    解释以查找实体

    注释
    @SpringBootApplication(scanBasePackages=“my.package”)
    在这里查找您的SpringBean,而不是您的实体

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

    Hibernate:删除表(如果存在用户) Hibernate:创建表用户(id_user varchar(255)不为null,email varchar(255),first_name varchar(255),language varchar(255),last_name varchar(255),password varchar(255),rol varchar(255),主键(id_user))


    让我知道。

    您使用的是哪个版本的spring boot?我有版本2.1.1,很抱歉,我的复制粘贴有问题,但我有您发送的代码,这并不能解决我的问题。您还有spring boot starter数据jpa依赖项吗?你可以试试:jpa:hibernate:ddl auto:created但是我在修改数据库之前没有关闭spring,是的,我有依赖性你有:show sql:true启动spring时它会输出什么?我没有你发送的日志,我在我的代码中添加了postgresapplication.java,让您可以看到它添加到了
    postgresapplication
    @EntityScan(“com.sample.modelo”)
    注释我正试图使用设计赞助人MVC,所以com.sample.modelo是我想把我所有的实体都放在这里的地方,是这样!!!非常感谢你的帮助,如果你把它添加到你的答案中,我可以把它作为正确的答案。嗯,我做了,但我会把它说得更清楚
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    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
    
    
    @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(){
    
        }
    }
    
    package your.package;
    
    @SpringBootApplication
    public class MainApp {
    
        public static void main(String[] args) {
            SpringApplication.run(StackTest.class);
        }
    
    }
    
    
    @SpringBootApplication
    @EntityScan("my.other.package")
    public class MainApp {
    
    }
    
    Configuration configuration = new Configuration();
    // many codes here
    configuration.addPackage("my.package.for.entities");