我在Google / MyBatis论坛上广泛浏览了这个错误,但找不到明确的答案。我的表定义如下:
CREATE TABLE "user"
(
id integer NOT NULL DEFAULT nextval('user_userid_seq'::regclass),
username character varying(256) NOT NULL,
first_name character varying(128),
last_name character varying(128),
id_number character varying(32),
id_type integer,
email_verified boolean,
cellphone_verified boolean,
token character varying(256),
CONSTRAINT pk_id PRIMARY KEY (id),
CONSTRAINT unique_user_username UNIQUE (username)
)
WITH (
OIDS=FALSE
);
user_userid_seq定义为:
CREATE SEQUENCE user_userid_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
我使用MyBatis生成器生成MapperXML和其他类。但是,当我尝试使用自动生成的映射器插入记录时,出现以下错误:
Error updating database. Cause: org.postgresql.util.PSQLException:
ERROR: null value in column "id" violates not-null constraint
Detail: Failing row contains (null, abc@gmail.com, Edwin, Eliot, 98098098, 1, f, null, null).
The error may involve com.dukstra.xxx.web.data.mapper.UserMapper.insert-Inline
The error occurred while setting parameters
SQL: insert into public.user (id, username, first_name, last_name, id_number, id_type, email_verified, cellphone_verified, token ) values (?, ?, ?, ?, ?, ?, ?, ?, ? )
Cause: org.postgresql.util.PSQLException:
ERROR: null value in column "id" violates not-null constraint
Detail: Failing row contains (null, abc@gmail.com, Edwin, Eliot, 98098098, 1, f, null, null).
似乎在打电话时 mapper.insert(NEWUSER); id列被传递为null,这会导致上面的错误。解决这个问题的最佳方法是什么?我发现了几个工作,但不确定是否有明确的方法?某些解决方案建议使用“generatedKey”选项指定显式selectKey并将类型设置为“pre”。但是,这意味着必须使用带序列的自动生成的主键为每个表编写/自定义xml。有没有更简单的方法告诉Postgres如果值为null,则自动使用默认键,或者在生成的用户对象的ID字段中允许“DEFAULT”(String)值? 处理这个问题的最佳方法是什么?
更新:这是生成器配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="useGeneratedKeys" value="true" />
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5433/travelnet" />
<property name="username" value="postgres" />
<property name="password" value="*******" />
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.dukstra.travelnet.web.data.mapper" />
</mappers>
答案 0 :(得分:2)
我认为最好的方法是在执行插入时不将 id 列传递给DB。新记录将从您拥有的序列中获取 id 值。您使用这些特定技术可以实现这一目标,我不确定,因为我对它们没有经验。可能你可以通过MapperXML或你拥有的实体类中的一些注释来做到这一点。
答案 1 :(得分:0)
使用BIGSERIAL代替创建手动序列。
CREATE TABLE "user"
(
id BIGSERIAL,
username character varying(256) NOT NULL,
first_name character varying(128),
last_name character varying(128),
id_number character varying(32),
id_type integer,
email_verified boolean,
cellphone_verified boolean,
token character varying(256),
CONSTRAINT pk_id PRIMARY KEY (id),
CONSTRAINT unique_user_username UNIQUE (username)
)
WITH (
OIDS=FALSE
);