当主列具有自动递增时,我在通过Hibernate将数据插入Postgresql时遇到问题。 我也在这个论坛上经历过多个帖子,但找不到适合我的解决方案。
我的表和序列在“apiprofile”模式中定义。当我运行代码时,它无法找到序列名称。 即使我用schema.sequence提及它,它仍然不起作用。
感谢任何帮助。
以下是我所面临的代码段和异常。
序列&表:
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文件
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;
插入操作
LoginSession session = new LoginSession();
session.setUsername(userName);
session.setToken(token);
session.setActive(true);
session.setLoginTime(new Timestamp(System.currentTimeMillis()));
getSession().saveOrUpdate(session);
异常
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:?]