java amp 255_关于java:Spring Boot&

我有两个实体: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;

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;

}

}

我得到这个错误:

Error executing DDL via JDBC StatementCaused by:

org.postgresql.util.PSQLException: ERROR: relation"sub_comment" does

not exist

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 = new HashSet<>();

@OneToMany(mappedBy ="comment", targetEntity = SubComment.class)

public Set getSubComment() {

return subComment;

}

public void setSubComment(Set 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文件中粘贴以下内容:

org.postgresql

postgresql

runtime

如需进一步参考,请参阅此stackoverflow post。

已尝试修复(复制并粘贴),结果相同…org.postgresql.util.PSQLException: ERROR: relation"sub_comment" does not exist。

现在看看我编辑过的答案。

添加了另一个编辑。它当然可以通过删除现有表并创建新表来工作。

如果解决方案有效,那么就接受这个答案作为解决方案,并对其进行投票。

同样的结果,sub_comment does not exist。为什么会因为基于字段的访问而出错?您何时使用基于字段的与基于属性的?

好的,再次查看编辑过的答案

将application.properties文件内容添加到问题中。

已更新以包含application.properties

好了,现在把我在答案中建议的内容加起来。像我提到的那样粘贴整个实体类代码(comment,subcomment),有时还可以对整个实体类代码进行注释。

许多子注释属于一个注释,模型应该是这样的(您可以生成的注释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

我想请您看看stackoverflow解决方案stackoverflow.com/a/44055520/7538821

你的问题有什么共同点?如果您的意思是我现在想念getter/setter,lombok处理这个问题,我只提供了一个通用的解决方案,而不是整个实现。

如果您使用多对一注释进行基于字段的属性访问,那么项目甚至不会编译,而且这个bug仍然处于Hibernate5中。基于属性的访问是唯一的解决方案。

您可以参考这里的关于java. org/& Helip的想法;

不幸的是,你错了。我不知道HibernateMagic在引擎盖下做了什么,但是join列名称可以有一个存在于db中的名称。这是我在RealEnv上工作的代码。

阅读问题。用户表示pojo需要双向的一对多关系,这也是为什么需要在另一端提到@one to many注释的原因。

哦,对不起,我在之前的评论中提到了@manytoone,这是不应该的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值