当主列具有自动增量时,我在通过 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)
);
豆文件
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:?]