A memo to be taken when an error occurs when using a sequence in the ID column of PostgreSQL + JPA.
With PostgreSQL (10.0)
create table employee (
employee_id serial not null,
employee_name text,
constraint employee_PKC primary key (employee_id)
);
When there is a table like
@Entity
public class Employee {
@Id
@SequenceGenerator(name = "employee_employee_id_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer employeeId;
private String employeeName;
// Getter/Setter
}
If you add the annotation, when saving the record
org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist
I got the error. Apparently, the default sequence name is referenced.
There are two ways.
The drawback is that you have to write the sequence name many times.
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_employee_id_seq")
@SequenceGenerator(name = "employee_employee_id_seq", sequenceName = "employee_employee_id_seq")
private Integer employeeId
This is simpler.
@Id
@SequenceGenerator(name = "employee_employee_id_seq")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer employeeId;