【问题标题】:I can´t create tables in my database postgresql with springboot我无法使用 spring boot 在我的数据库 postgresql 中创建表
【发布时间】:2020-01-14 15:19:12
【问题描述】:

我正在尝试 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  

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

用户.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

【问题讨论】:

  • 你用的是哪个版本的spring-boot?
  • 我的版本是2.1.1

标签: postgresql hibernate spring-boot


【解决方案1】:

简答:在PostgresApplication 类的顶部添加@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);

    }

}

更多信息:

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

  1. 使用此依赖项
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
  1. src/main/resources 中有你的application.properties
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 {

}

更多关于休眠配置

在没有 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,而不是您的实体。

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

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

告诉我。

【讨论】:

  • 我没有您发送的日志,我在代码中添加了 postgresapplication.java 供您查看
  • 添加到您的PostgressApplication @EntityScan("com.sample.modelo") 注释
  • 我正在尝试使用设计赞助人 MVC,所以 com.sample.modelo 是我想放置所有实体的地方
  • 是吗!!!非常感谢您的帮助,如果您将其添加到您的答案中,我可以将其作为正确答案
  • 嗯,我做到了,但我会说得更明显
【解决方案2】:

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

接下来:查看您的属性文件:

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

【讨论】:

  • 对不起,是我复制粘贴的问题,但我有你发给我的代码
  • 这不能解决我的问题
  • 你也有 spring-boot-starter-data-jpa 依赖?你可以试试:jpa:hibernate:ddl-auto:create
  • 但在修改数据库之前我不会关闭 spring,是的,我有那个依赖项
  • 你有:show-sql: true 启动Spring时输出什么?
【解决方案3】:

尝试只使用 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

【讨论】:

    猜你喜欢
    • 2021-02-09
    • 2021-10-30
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2017-11-11
    相关资源
    最近更新 更多