我嘗試在休眠中學習一對多(單向)關系。我創建了一個帶有帖子和評論模型的簡單專案。我正在嘗試使用 sql 檔案在 postgres 中初始化我的資料庫,但 hibernate 無法創建 post 表。任何人都可以告訴我我做錯了什么?我正在嘗試使用 stackoverfow 的一些解決方案,但它不起作用,或者我犯了一些愚蠢的錯誤。感謝幫助。
這是我得到的錯誤:
org.springframework.beans.factory.BeanCreationException:創建名為“dataSourceScriptDatabaseInitializer”的bean在類路徑資源[org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]中定義時出錯:呼叫init方法失敗;嵌套例外是 org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [data.sql]: INSERT INTO post(title, content, created) values ('dupa', '內容 1', CURRENT_TIMESTAMP); 嵌套例外是 org.postgresql.util.PSQLException:錯誤:“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>
uj5u.com熱心網友回復:
你需要在物體類中給出表名
@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() {
}
}
uj5u.com熱心網友回復:
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 中的一些選項。最終這種形式對我有用,但這不能很好地解決我的問題,因為這一行:
不推薦使用的選項初始化模式
在我的 spring 版本中已棄用。我應該使用:
spring.sql.init.mode=always
我現在不知道為什么它適用于 depricated 選項。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/337483.html
標籤:爪哇 春天 PostgreSQL 弹簧靴 休眠
