【发布时间】: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