I have two entities: Comment
, and SubComment
. A Comment
can have multiple SubComment
s. I'm trying to establish a one to many/many to one bidirectional relationship with Hibernate.
I do not know what is wrong. Both of the tables seem to have been created correctly in PSQL.
Comment.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;
}
}
SubComment.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;
}
}
I'm getting this error:
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
application.properties
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
Java Spring Boot (Spring Boot) is a tool that makes developing web application and microservices with Spring Framework faster and easier through three core capabilities: Autoconfiguration. An opinionated approach to configuration. The ability to create standalone applications.
Spring Boot is an open source Java-based framework used to create a micro Service. It is developed by Pivotal Team and is used to build stand-alone and production ready spring applications.
Spring Boot is a lightweight framework that takes most of the work out of configuring Spring-based applications. In this tutorial, you'll learn how to use Spring Boot's starters, opinions, and executable JAR file structure to quickly create Spring-based applications that "just run."
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications. It's a Java-based framework used to create a microservice ( microservice is defined as the small services that work together.
You missed @JoinColumn
. You will get another error due to field based access. Use Property based access instead:
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;
}
}
Also make changes here too:
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;
}
}
Paste the following in your application.properties
file:
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
In your pom.xml file paste these:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
For further reference see this stackoverflow post.
I know that this answer is late but maybe it will help someone, their problem is that they have not configured the schema, which is why they are throwing that error.
Here is how to configure the schema in postgresql:
spring.jpa.properties.hibernate.default_schema = "his schema"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With