Foxes
Foxes

Reputation: 69

Hibernate @Column annotation cannot mapping to database

I have a Entity in Spring Boot and PostgreSql, I'm using @Column annotation to mapping to database. This is my Entity snip code :

@Entity(name = "users")
@Table(name = "users", schema = "public")
public class User implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 12355345L;
    @Id
    @Column(name = "user_id")
    private String userid;
    @Column(name = "user_name")
    private Integer username;

When id run and test with postman, i get an error :

org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

 Caused by: org.postgresql.util.PSQLException: ERROR: column users0_.usersid does not exist
  Hint: Perhaps you meant to reference the column "users0_.user_id".

I don't know why. How to resolve this ?

Upvotes: 2

Views: 3995

Answers (1)

Tharsan Sivakumar
Tharsan Sivakumar

Reputation: 6531

There are couple of things for your info.

1) Need to check your spring.jpa.hibernate.ddl-auto property as depends on that property, the database tables, columns will be populated by Hibernate.

2) Next drop the existing table and change the value as spring.jpa.hibernate.hbm2ddl.auto=update so that it will create/update table according to the annotations provided in the entity class

3) Remove unnecessary annotations. Following is enough.

@Entity
@Table(name = "users")
public class User implements Serializable {

 @Id
 @Column(name = "user_id")
 private String userid;

 ................

Upvotes: 2

Related Questions