【问题标题】:Spring can't initialize database in postgresSpring无法在postgres中初始化数据库
【发布时间】:2021-12-04 10:20:18
【问题描述】:

我尝试在休眠中学习一对多(单向)关系。 我创建了一个带有帖子和评论模型的简单项目。 我正在尝试使用 sql 文件在 postgres 中初始化我的数据库,但 hibernate 无法创建 post 表。 谁能告诉我我做错了什么? 我正在尝试使用 stackoverfow 的一些解决方案,但它不起作用,或者我可能犯了一些愚蠢的错误。感谢您的帮助。

这是我得到的错误:

org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class] 中定义名称为“dataSourceScriptDatabaseInitializer”的 bean 创建错误:调用 init 方法失败;嵌套异常是 org.springframework.jdbc.datasource.init.ScriptStatementFailedException: 未能执行类路径资源 [data.sql] 的 SQL 脚本语句 #1: INSERT INTO post(title, content, created) values ('dupa', '内容 1', CURRENT_TIMESTAMP);嵌套异常是 org.postgresql.util.PSQLException: ERROR: the "post" 关系不存在

这是我创建的模型、存储库、服务和控件:

     @Entity
        @Getter
        @Setter
        public class Post {
            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            private Long id;
            private String title;
            private String content;
            private LocalDateTime created;
            @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
            private List<Comment> comments;
        
            public Post() {
            }
        }
    
        @Entity
        @Getter
        @Setter
        public class Comment {
            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            private Long id;
            private String content;
            private LocalDateTime created;
        
            public Comment() {
            }
        }
    
            @Repository
        public interface PostRepository extends JpaRepository<Post, Long> {
        }
    
    @Service
    @AllArgsConstructor
    public class PostService {
    
        private final CommentRepository commentRepository;
        private final PostRepository postRepository;
    
        public List<Post> getPosts(){
            return postRepository.findAll();
        }
    }
    
        @RestController
        @RequiredArgsConstructor
        public class PostController {
            private final PostService postService;
            private final CommentService commentService;
        
            @GetMapping("/posts")
            public List<Post> getPosts(){
                return postService.getPosts();
            }
        }
    

这是我的 application.properties:

spring.jpa.hibernate.ddl-auto=create-drop
spring.sql.init.mode=always
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postcommentapi
spring.datasource.username=amso
spring.datasource.password=1234
spring.sql.init.data-locations = classpath:/create_db_content
spring.jpa.show-sql=true

我的 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 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>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.konrad</groupId>
    <artifactId>postcommentapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>postcommentapi</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</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-data-jpa</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

  • create_db_content 文件放在哪里?
  • 在资源文件夹中。

标签: java spring postgresql spring-boot hibernate


【解决方案1】:

需要在实体类中给出表名

@Entity
@Getter
@Setter
@Table(name = "post")
        public class Post {
            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            private Long id;
            private String title;
            private String content;
            private LocalDateTime created;
            @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
            private List<Comment> comments;
        
            public Post() {
            }
        }

也在评论实体中

@Entity
@Getter
@Setter
@Table(name = "comment")
        public class Comment {
            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            private Long id;
            private String content;
            private LocalDateTime created;
        
            public Comment() {
            }
        }

【讨论】:

  • 不幸的是,它对我没有帮助。仍然有同样的错误。
  • 在 application.properties 文件 spring.jpa.hibernate.ddl-auto=create 中将 create-drop 更改为 update 尝试一次
  • 我粘贴了我在 application.properties 中所做的更改,它可以工作,休眠创建表并向它们添加记录。我仍然不知道为什么 hibernate 使用这个选项创建表:spring.datasource.initialization-mode=always 在我的 spring 版本中已弃用。
  • 试试这个spring.sql.init.mode=never
  • 就像您在上面看到的,我没有更改 spring.jpa.hibernate.ddl-auto= 中的任何选项= 所以,我认为该选项没有问题。在某种程度上,我解决了创建表并将数据插入其中的问题。我想知道为什么 hibernate 在不推荐使用的版本上使用初始化数据库选项而不是专用版本。
【解决方案2】:
spring.sql.init.platform = postgres
spring.datasource.url = jdbc:postgresql://localhost:5432/postcommentapi
spring.datasource.username =
spring.datasource.password =
spring.datasource.driver-class-name = org.postgresql.Driver

spring.jpa.show-sql = true
spring.jpa.generate-ddl = true
spring.jpa.hibernate.ddl-auto = create
spring.datasource.initialization-mode = always
#spring.sql.init.mode = always
spring.sql.init.data-locations = classpath:/import.sql
spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect

我试图更改 applications.properties 中的一些选项。 最终这种形式对我有用,但这并不能很好地解决我的问题,因为这条线:

deprecated option initialization mode

在我的 spring 版本中已弃用。我应该使用:

spring.sql.init.mode = always

我现在不明白为什么它适用于已弃用的选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2023-04-08
    • 2019-08-08
    相关资源
    最近更新 更多