postgresqljpa

springboot look for wrong sequence to insert into postgres DB


I have an entity;

@Entity
@Table(name = "organization")
public class Organization {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE)
  private long id;

  @Column(name = "name")
  private String name;
.
.
.

I created the table:

create table organization (
    id int generated always as identity primary key,
    name text unique not null
);

As a result of this query, organization_id_seq is created in the db.

When I send request and try to insert data, it fails with the error message

jdbc.spi.SqlExceptionHelper: ERROR: relation "organization_seq" does not exist

organization_seq really does not exist but organization_id_seq exists, how can i fix this?

I have tried GenerationType.AUTO the result was the same error.


Solution

  • Set the GeneratedValue.strategy to GenerationType.IDENTITY.

    This is the correct strategy to use when the table has an identity column for the PK.

    What AUTO does depends on the datatype of the PK and the database and in your case seems to end up trying to use a sequence. If you want to use a sequence create a sequence with the proper name in your schema creation script and drop the identity declaration of the id column.

    See for example https://www.baeldung.com/hibernate-identifiers for more about how ids can get generated.