Reputation: 545
I have this class User in my Spring project
@Entity
public class User extends TimeStampModel{
// Extends TimeStampModel so we know when the user was created
@Id
@GeneratedValue
@Column
private long id;
@Column
private String username;
@Column
private String password;
@Column
private long amountOfLikes;
@Column
private long amountOfDislikes;
@OneToMany
private List<Comment> comments;
public User(){}
//getters and setters omitted
}
But I am a getting a error: Caused by: org.postgresql.util.PSQLException: ERROR: relation "comment" does not exist
along with many other errors. All my other tables are getting created correctly.
What am I doing wrong here?
Here's my comment class, Likeable is an abstract class which has Columns for numberOfLikes and numberOfDislikes.
@Entity
public class Comment extends Likeable {
@Id
@Column
@GeneratedValue
private long id;
@Column(nullable = false)
@Lob
private String text;
@OneToOne
private User author;
@OneToMany(cascade = CascadeType.ALL)
private List<Comment> childComments;
public Comment(){}
//getters and setters omitted
}
Here's also the Likeable abstract class if necessary
@MappedSuperclass
public abstract class Likeable extends TimeStampModel{
@Column
private int likeAmount;
@Column
private int dislikeAmount;
public void increaseLikeAmount(){
this.likeAmount++;
}
public void increaseDislikeAmount(){
this.dislikeAmount--;
}
//getters and setters omitted
}
Here are my application.properties also
spring.jpa.database=POSTGRESQL
spring.datasource.url=jdbc:postgresql://localhost:5432/Test
spring.datasource.username=user
spring.datasource.password=pass
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
Upvotes: 0
Views: 784
Reputation: 755
I see some problems in your code.
a) Your mapping is not correct. In User you have 1 to many mapping with comments while in comment you have one to one mapping with user. This needs to be rectified.
b) Please try using @Table over top of both user and comment class to rename your table names as user is a keyword. It will not work without that.
You may also want to use mappedby attribute with bidirectional mapping to prevent unnecessary columns.
Upvotes: 0
Reputation: 755
Did you set hbm2ddl.auto to create for it to create tables, also you may need to add the mapping for your entity classes.
Upvotes: 0
Reputation: 2028
In your User entity, you are doing @OneToMany
relation to Comment but in your Comment entity it is @OneToOne.
Change it to @ManyToOne.
Upvotes: 2
Reputation: 545
User is a reserved word in PostgreSQL, changing it to something else fixed everything.
Upvotes: 2