我正在尝试使用post
实现spring data jpa
请求。我正在尝试使用一些值在房间表中添加新房间。房间表中的某些列不为空,我将其设置为默认值。但是当我插入时,我得到的错误为violates not-null constraint
。
RoomService类
// Add new Room Details
public String addNewRoom(@RequestBody RoomInformation roomInfo) {
Room roomRecord = new Room();
if(roomInfo.nCampusId != 0)
roomRecord.nCampusId = roomInfo.nCampusId;
if(roomInfo.nBuildId != 0)
roomRecord.nBuildId=roomInfo.nBuildId;
if(roomInfo.nCRTCodeId !=0)
roomRecord.nCRTCodeId=roomInfo.nCRTCodeId;
roomRecord.nInstId=roomInfo.nInstId;
roomRecord.sRoomNumber=roomInfo.sRoomNumber;
roomRecord.sRoomDesc=roomInfo.sRoomDesc;
roomRecord.nArea=roomInfo.nArea;
roomRecord.sFloor=roomInfo.sFloor;
roomRecord.bIsActive= true;
roomRepository.save(roomRecord);
return "New room added sucessfully";
}
房间桌子
CREATE TABLE public.room
(
nroom_id numeric(18,0) NOT NULL DEFAULT nextval('room_seq'::regclass),
ncampus_id numeric(18,0),
nbuild_id numeric(18,0),
ninst_id numeric(18,0) NOT NULL DEFAULT 0,
sfloor character varying(10) COLLATE pg_catalog."default",
sroom_number character varying(10) COLLATE pg_catalog."default",
sroom_desc character varying(255) COLLATE pg_catalog."default",
scomments text COLLATE pg_catalog."default",
daccepted_date timestamp(3) without time zone,
ssurveyor character varying(255) COLLATE pg_catalog."default",
narea integer,
ncrt_code_id numeric(18,0),
ncmn_room_bln smallint,
nunvalidated_bln smallint,
sbfr_field character varying(50) COLLATE pg_catalog."default",
ntemp_room_id numeric(18,0),
bis_active boolean NOT NULL DEFAULT false,
bis_jointuse boolean NOT NULL DEFAULT false,
sstations_desc character varying(25) COLLATE pg_catalog."default",
ddeleted_on timestamp(3) without time zone,
ndeleted_by numeric(18,0),
bis_incluster boolean NOT NULL DEFAULT false,
bis_service_center_activity boolean NOT NULL DEFAULT false,
service_center_comments text COLLATE pg_catalog."default",
CONSTRAINT pk_roomdeails PRIMARY KEY (nroom_id),
)
控制台错误
“ bis_jointuse”列中的空值违反了非空约束
Detail: Failing row contains (1203521, 270, 11135, 106, 0, 10, abc, null, null, null, 22, 2122, null, null, null, null, t, null, null, null, null, null, null, null).
答案 0 :(得分:1)
您需要跳过列或提供DEFAULT
关键字,而不要使用显式的NULL
:
INSERT INTO tab(col1,...,bis_jointuse)
VALUES (22, ..., DEFAULT);
-- skipping column
INSERT INTO tab(col1,...)
VALUES (22,...);
这些值在我设置为默认值的表中不为空。但是,当我尝试插入时,默认值没有采用。
据我所知,只有Oracle支持这种构造: DEFAULT Values On Explicit NULLs
在上一节中,我们看到仅当在insert语句中未引用列时才使用默认值。如果引用了该列,即使提供值NULL,也不会使用默认值。 Oracle 12c允许您使用默认定义中的ON NULL子句来修改此行为。
CREATE TABLE t2 (
col1 NUMBER DEFAULT 1,
col2 NUMBER DEFAULT ON NULL 2,
description VARCHAR2(30)
);