0

Ran into a peculiar problem. I am using JPA + spring to handle my entities. Data are saved in Postgresql. Tables are generated by JPA. I didn't create any of them.

One of the entity 'Family' has an ID field.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="family_id",nullable = false, columnDefinition = "BIGINT")
private Long id;

So, JPA (or hibernate) generate a column with a sequence 'family_id_seq'. It will query currval('family_id_seq') when new entry are being inserted. Everything works as expected.

Today when I debugging my program. Suddenly I cannot save any 'Family' using JpaRepository. The error message is currval('family_family_id_seq') does not exist.

Why does my JPA changed the generated seq name? How do I control this? Which class of Spring JPA is responsible for creating this query? I want my seq named 'family_id_seq'. Because other tables' seq is still following that rule.

Maxi Wu
  • 1,274
  • 3
  • 20
  • 38
  • Did you change anything in the app or on the database? – Simon Martinelli Jun 20 '19 at 09:37
  • Actually, I found that, if the table is dropped/recreated you can lose access to the sequence and then you receive this error. Check if the sequence itself exists in the DB. – Brother Jun 20 '19 at 09:56
  • 1
    Using `IDENTITY` under PostgreSQL (which I assume you're using) makes Hibernate leave the field for the database to insert the default. If you want the field to be generated by Hibernate, define it as `bigserial` instead of `bigint`. – coladict Jun 20 '19 at 14:19

0 Answers0