[英]Spring Boot & Postgres: relation "sub_comment" does not exist
我有兩個實體: Comment
和SubComment
。 一個Comment
可以有多個SubComment
。 我正在嘗試與 Hibernate 建立一對多/多對一的雙向關系。
我不知道出了什么問題。 這兩個表似乎都是在 PSQL 中正確創建的。
注釋.java
import javax.persistence.*;
import java.util.Set;
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
private String text;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "comment")
private Set<SubComment> subComment;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
子注釋.java
import javax.persistence.*;
@Entity
public class SubComment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String text;
@ManyToOne
private Comment comment;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Comment getComment() {
return comment;
}
public void setComment(Comment comment) {
this.comment = comment;
}
}
我收到此錯誤:
通過 JDBC 語句執行 DDL 時出錯引起:org.postgresql.util.PSQLException:錯誤:關系“sub_comment”不存在
Hibernate: create table "user" (id bigserial not null, email varchar(255), name varchar(255), username varchar(255), primary key (id))
Hibernate: create table comment (comment_id bigserial not null, text varchar(255), primary key (comment_id))
Hibernate: create table sub_comment (sub_comment_id bigserial not null, text varchar(255), comment_comment_id int8, primary key (sub_comment_id))
Hibernate: alter table sub_comment add constraint FK87789n34vmns9eeyw6jgc5ghp foreign key (comment_comment_id) references comment
應用程序屬性
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.data-username=username
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
你錯過了@JoinColumn
。 由於基於字段的訪問,您將收到另一個錯誤。 改用基於屬性的訪問:
import javax.persistence.*;
@Entity
@Table(name = "subcomment")
public class SubComment implements Serializable {
private static final long serialVersionUID = -3009157732242241606L;
private long id;
private String text;
private Comment comment;
@Id
@Column(name = "sub_id")
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "sub_text")
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@ManyToOne
@JoinColumn(name = "sub_fk_c_id", referencedColumnName = "c_id") // here the exact field name of your comment id in your DB
public Comment getComment() {
return comment;
}
public void setComment(Comment comment) {
this.comment = comment;
}
}
也在這里進行更改:
import javax.persistence.*;
@Entity
@Table(name = "comment")
public class Comment implements Serializable {
private static final long serialVersionUID = -3009157732242241606L;
private long id;
private String text;
private Set<SubComment> subComment = new HashSet<>();
@OneToMany(mappedBy = "comment", targetEntity = SubComment.class)
public Set<SubComment> getSubComment() {
return subComment;
}
public void setSubComment(Set<SubComment> subComment) {
this.subComment = subComment;
}
@Id
@Column(name = "c_id")
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "c_text")
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
將以下內容粘貼到您的application.properties
文件中:
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.jpa.hibernate.ddl-auto=create
在你的 pom.xml 文件中粘貼這些:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
如需進一步參考,請參閱此 stackoverflow 帖子。
我知道這個答案來晚了,但也許它會幫助某人,他們的問題是他們沒有配置架構,這就是他們拋出該錯誤的原因。
以下是如何在 postgresql 中配置架構:
spring.jpa.properties.hibernate.default_schema = "his schema"
許多子評論屬於一個評論,模型應該看起來像這樣(comment_id 你可以生成例如 UUID.randomUUID().toString() )
@Entity
@Table(name = "comment")
public class Comment{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "comment_id")
String commentId;
}
@Entity
@Table(name = "sub_comment")
public class SubComment{
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "comment_id", referencedColumnName = "comment_id")
Comment comment;
...
}
如果您使用例如liquidbase 來控制您的數據源:
- changeSet:
id: 1
author: author.name
changes:
- addForeignKeyConstraint:
baseColumnNames: comment_id
baseTableName: sub_comment
constraintName: fk_comment_subcomment
referencedColumnNames: comment_id
referencedTableName: comment
有一個類似的問題,我的問題是我的 SubCategory 類下缺少 referencedColumnName。
有這個
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinColumn(name = "category_id, nullable = false)
private Category category;
代替
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinColumn(name = "category_id", referencedColumnName = "id", nullable = false)
private Category category;
所以我的子類別表沒有被創建。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.