What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA

A memo to be taken when an error occurs when using a sequence in the ID column of PostgreSQL + JPA.

phenomenon

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.

Coping

There are two ways.

How to explicitly specify the sequence name

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

How to use GenerationType.IDENTITY

This is simpler.

@Id
@SequenceGenerator(name = "employee_employee_id_seq")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer employeeId;

Recommended Posts

What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA
What to do when rails db: seed does not reflect in the database
What to do when the changes in the Servlet are not reflected
What to do when Rails on Docker does not reflect controller changes in the browser
[Grails] Error occurred running What to do when the Grails CLI does not start
Processing when an ID that does not exist in the database is entered in the URL
What to do when Method not found in f: ajax
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
[Rails] What to do when rails s does not respond or does not stop
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
What to do if the prefix c is not bound in JSP
How to constrain the action of the transition destination when not logged in
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do when debugging "Source not found"
What to do when IllegalStateException occurs in PlayFramework
[JPA] Compare table1 and table2 to get and update data that does not exist in table2.
What to do if the changes are not reflected in the jar manifest file
What to do when the value becomes null in the second getSubmittedValue () in JSF Validator
What to do when is invalid because it does not start with a'-'
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
What to do if the update does not take effect after deploying Rails AWS
[React.useRef] What to do when the latest state cannot be referenced in the event listener
What to do when The SSL certificate has expired
What to do when JSF tags do not become HTML
What to do if you can't get the text of an element in Selenium
What to do when "Fail to load the JNI shared library" is displayed in Eclipse
What to do if the JSONHint annotation does not work with lombok and JSONIC
What to do if the breakpoint is shaded and does not stop during debugging
"Do not show again" check does not work in the warning dialog when starting applet
What to do if you select a JRE in Eclipse and get "The selected JRE does not support the current compliance level 11"
One of the causes and solutions when whales do not appear in Docker Quickstart Terminal
What to do when Cannot format given Object as a Date in convertDateTime of JSF
What to do if you installed Ruby with rbenv but the version does not change
What to do when a could not find driver appears when connecting to a DB in a Docker environment
Bluemix Infrastructure VPN does not connect because it does not support NPAPI! What to do when [Mac]
What to check when rails db: migration does not pass
How to get the log when install4j does not start
What to do if Could not find hoge in any of the sources Run `bundle install` to install missing gems. Appears in the docker container
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
[Programming beginner] What to do when rails s becomes an error in the local development environment
What to do when you want to know the source position where the method is defined in binding.pry
In order not to confuse the understanding of getters and setters, [Do not use accessors for anything! ]
[Rails 5] [Turbolinks] What to do when JS does not work due to page transition or browser back
What to do if the app is not created with the latest Rails version installed when rails new
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed
[Ubuntu 20.04] What to do if the external monitor is not recognized
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
[Rails] What to do if data is not registered in DB
Handling when calling a key that does not exist in hash
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if Cloud9 is full in the Rails tutorial
How to get the id of PRIMAY KEY auto_incremented in MyBatis
What to do if you forget the root password in CentOS7
If you do not call shutdownNow when the transfer is completed in the Java SDK of AWS S3, threads will continue to remain
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
What to do when javax.batch.operations.JobStartException occurs
What I did when I was addicted to the error "Could not find XXX in any of the sources" when I added a Gem and built it
[IOS] What to do when the image is filled with one color
What to do if the background image is not applied after deployment
What to do when Blocked Host: "host name" appears in Ruby on Rails