Error in Sequence lookup in Hibernate for Postgresql

232 views Asked by At

I am having problem in inserting data into Postgresql through Hibernate when Primary column has auto-increment. I have gone through multiple post in this forum also, but can't find a solution that works for me.

My Table and Sequence are defined in "apiprofile" schema. When I run the code, it is unable to find the Sequence name. Even if I mention it with the schema.sequence, still it doesn't work.

Any help is appreciated.

Below are the code snippet and exception I am facing.

Sequence & Table:

CREATE SEQUENCE apiprofile.login_session_id_seq;

CREATE TABLE apiprofile.login_session (
  id bigint NOT NULL DEFAULT nextval('apiprofile.login_session_id_seq'),
  username varchar(255) NOT NULL,
  token varchar(500) NOT NULL,
  active_ind boolean NOT NULL,
  login_time timestamp NOT NULL,
  PRIMARY KEY (id)
);

Bean file

public class LoginSession {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "session_generator")
@SequenceGenerator(name="session_generator", sequenceName = "apiprofile.login_session_id_seq", schema = "apiprofile", allocationSize=1) 
@Column(name="id", updatable = false, nullable = false)
private Integer id;

Insert operation

LoginSession session = new LoginSession();
session.setUsername(userName);
session.setToken(token);
session.setActive(true);
session.setLoginTime(new Timestamp(System.currentTimeMillis()));
getSession().saveOrUpdate(session);

Exception

Hibernate: 
    select
        next_val as id_val 
    from
        login_session_id_seq for update

could not read a hi value
org.postgresql.util.PSQLException: ERROR: relation "login_session_id_seq" does not exist
  Position: 32
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103) ~[postgresql-9.1-901-1.jdbc4.jar:?]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836) ~[postgresql-9.1-901-1.jdbc4.jar:?]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) ~[postgresql-9.1-901-1.jdbc4.jar:?]
0

There are 0 answers