Spring : java.sql.SQLException: 字段“**”没有默认值
Spring : java.sql.SQLException: Field '**' doesn't have a default value
我的实体是一个数据 class 具有默认值并且 json 正在 post api 调用中的服务器中接收但只有一个字段,即由于某种原因,数据中不存在 class 始终为 null。
@Entity
@Table(name = "checking_point")
data class CheckingPointEntity(
@Column(nullable = false, unique = false, length = 500)
open var name: String = "",
@Column(nullable = false, unique = false, length = 500)
open var description: String = "",
@Column(nullable = false)
open var cpTypeId: Int = 0,
@Column(nullable = false)
open var isCumulative: Boolean = false,
@Column(nullable = false)
open var cpCategoryGroupId: Long = 0L,
@Column(nullable = false)
open var isTemplate: Boolean = false,
@Column(nullable = false)
open var isTopTemplate: Boolean = false
) : BkpBaseEntity1()
BastEntity1 :
@MappedSuperclass
open class BkpBaseEntity1 {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
open var id:Long = 0L
// **************************************************************************
// ***** Below 4 columns should be there in each and every table *******
// **************************************************************************
@Column(nullable = false, updatable = false, columnDefinition = "varchar(100) default 'adhoc-sql'")
open var dbCreateSource: String = BkpConstants.DB_CREATE_SOURCE_NAME;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" )
@Temporal(TemporalType.DATE)
@Column(nullable = false,insertable = false, updatable=false, columnDefinition = "datetime DEFAULT now()")
open var dbCreateDts: Date = Date() //Do not set this field. It will be populated by DB.
@Column(nullable = false, columnDefinition = "varchar(100) default 'adhoc-sql'")
open var dbUpdateSource: String = BkpConstants.DB_CREATE_SOURCE_NAME;
@Column(nullable = false, insertable = false, updatable=false, columnDefinition = "datetime NOT NULL DEFAULT now() on update now()")
@Temporal(TemporalType.DATE)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
open var dbUpdateDts: Date = Date() //Do not set this field. It will be populated by DB.
}
控制器:
@RequestMapping(path = [ControllerEndPoints.AddCheckingPoint], method = [RequestMethod.POST])
fun addCheckingPoint(@RequestBody(required = true) reqData: ChartServerVo): ResponseEntity<ChartAck> {
mLog("in add cp ")
var cpId = 0L
var pcId = 0L
val cpDto : CheckingPointDto = reqData.checkingPointDto
val gson = GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()
val payload = gson.toJson(reqData)
mLog("json: \n json $payload ")
mLog("json: saved cpDto name ${cpDto.name}, description ${cpDto.description} cpTypeId ${cpDto.cpTypeId} isCumulative ${cpDto.isCumulative} categoryGroupId ${cpDto.cpCategoryGroupId} isTemplate")
cpr.save(cpDto.toEntity())
...................
}
服务器收到的Json有所有字段
{"checkingPointDto":{"name":"demo","description":"","cpTypeId":1,"isCumulative":false,"cpCategoryGroupId":16,"isTemplate":false,"isTopTemplate":false,"id":0,"dbCreateSource":"","dbCreateDts":"2022-03-08 21:07:32","dbUpdateSource":"","dbUpdateDts":"2022-03-08 21:07:32"},"purusharthChartCpMappingDto":{"id":0,"purusharthChartId":16466443,"cpId":0},"purusharthChartDto":{"id":16466443,"adminID":8,"name":"demo","description":"","userId":8,"startDate":"2022-03-08 21:07:32","endDate":"2022-12-30 21:07:32","isSelfChart":false},"isNewchart":false}
错误表明不在实体中任何位置的字段 cp_type_id
为空。
java.sql.SQLException: Field 'cp_type_id' doesn't have a default value
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.25.jar:8.0.25]
我唯一能找到该字段的其他地方是在 .sql 文件中。
我是 spring 的新手,我不知道它应该做什么。就我的 sql 知识而言,我觉得还不错
create table checking_point(
id int primary key NOT NULL auto_increment,
name_str_id int,
foreign key(name_str_id) references localization(id),
description_str_id varchar(500),
cp_type_id int,
foreign key(cp_type_id) references dim_cp_type(id),
cp_category_group_id int,
foreign key(cp_category_group_id) references cp_category_group(id),
repeat_type_id int,
foreign key(repeat_type_id) references dim_repeat_type(id),
cp_repeat_reminder_id int,
is_template boolean,
is_top_template boolean,
db_create_dts date,
db_create_source nvarchar(320),
db_updated_dts date,
db_update_source nvarchar(320)
);
我错过了什么?为什么所提交的文件甚至不存在于导致此问题的相关实体中?
您是否尝试过通过SQL查询为其分配默认值?
如果您已经有一个数据库 GUI(如 MySQL workbench),您只需执行此附加查询即可将字段设置为具有默认值。
ALTER TABLE `checking_point` ALTER `cp_type_id` SET DEFAULT NULL
或者您可以更改您已有的sql查询
create table checking_point(
id int primary key NOT NULL auto_increment,
name_str_id int,
foreign key(name_str_id) references localization(id),
description_str_id varchar(500),
cp_type_id int DEFAULT NULL, <-HERE
foreign key(cp_type_id) references dim_cp_type(id),
cp_category_group_id int,
foreign key(cp_category_group_id) references cp_category_group(id),
repeat_type_id int,
foreign key(repeat_type_id) references dim_repeat_type(id),
cp_repeat_reminder_id int,
is_template boolean,
is_top_template boolean,
db_create_dts date,
db_create_source nvarchar(320),
db_updated_dts date,
db_update_source nvarchar(320)
);
通过分配 te 默认值,您将不再为 cp_type_id 获取这些类型的异常,同样的方法适用于它抱怨的任何其他字段。
我的实体是一个数据 class 具有默认值并且 json 正在 post api 调用中的服务器中接收但只有一个字段,即由于某种原因,数据中不存在 class 始终为 null。
@Entity
@Table(name = "checking_point")
data class CheckingPointEntity(
@Column(nullable = false, unique = false, length = 500)
open var name: String = "",
@Column(nullable = false, unique = false, length = 500)
open var description: String = "",
@Column(nullable = false)
open var cpTypeId: Int = 0,
@Column(nullable = false)
open var isCumulative: Boolean = false,
@Column(nullable = false)
open var cpCategoryGroupId: Long = 0L,
@Column(nullable = false)
open var isTemplate: Boolean = false,
@Column(nullable = false)
open var isTopTemplate: Boolean = false
) : BkpBaseEntity1()
BastEntity1 :
@MappedSuperclass
open class BkpBaseEntity1 {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
open var id:Long = 0L
// **************************************************************************
// ***** Below 4 columns should be there in each and every table *******
// **************************************************************************
@Column(nullable = false, updatable = false, columnDefinition = "varchar(100) default 'adhoc-sql'")
open var dbCreateSource: String = BkpConstants.DB_CREATE_SOURCE_NAME;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" )
@Temporal(TemporalType.DATE)
@Column(nullable = false,insertable = false, updatable=false, columnDefinition = "datetime DEFAULT now()")
open var dbCreateDts: Date = Date() //Do not set this field. It will be populated by DB.
@Column(nullable = false, columnDefinition = "varchar(100) default 'adhoc-sql'")
open var dbUpdateSource: String = BkpConstants.DB_CREATE_SOURCE_NAME;
@Column(nullable = false, insertable = false, updatable=false, columnDefinition = "datetime NOT NULL DEFAULT now() on update now()")
@Temporal(TemporalType.DATE)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
open var dbUpdateDts: Date = Date() //Do not set this field. It will be populated by DB.
}
控制器:
@RequestMapping(path = [ControllerEndPoints.AddCheckingPoint], method = [RequestMethod.POST])
fun addCheckingPoint(@RequestBody(required = true) reqData: ChartServerVo): ResponseEntity<ChartAck> {
mLog("in add cp ")
var cpId = 0L
var pcId = 0L
val cpDto : CheckingPointDto = reqData.checkingPointDto
val gson = GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()
val payload = gson.toJson(reqData)
mLog("json: \n json $payload ")
mLog("json: saved cpDto name ${cpDto.name}, description ${cpDto.description} cpTypeId ${cpDto.cpTypeId} isCumulative ${cpDto.isCumulative} categoryGroupId ${cpDto.cpCategoryGroupId} isTemplate")
cpr.save(cpDto.toEntity())
...................
}
服务器收到的Json有所有字段
{"checkingPointDto":{"name":"demo","description":"","cpTypeId":1,"isCumulative":false,"cpCategoryGroupId":16,"isTemplate":false,"isTopTemplate":false,"id":0,"dbCreateSource":"","dbCreateDts":"2022-03-08 21:07:32","dbUpdateSource":"","dbUpdateDts":"2022-03-08 21:07:32"},"purusharthChartCpMappingDto":{"id":0,"purusharthChartId":16466443,"cpId":0},"purusharthChartDto":{"id":16466443,"adminID":8,"name":"demo","description":"","userId":8,"startDate":"2022-03-08 21:07:32","endDate":"2022-12-30 21:07:32","isSelfChart":false},"isNewchart":false}
错误表明不在实体中任何位置的字段 cp_type_id
为空。
java.sql.SQLException: Field 'cp_type_id' doesn't have a default value
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.25.jar:8.0.25]
我唯一能找到该字段的其他地方是在 .sql 文件中。 我是 spring 的新手,我不知道它应该做什么。就我的 sql 知识而言,我觉得还不错
create table checking_point(
id int primary key NOT NULL auto_increment,
name_str_id int,
foreign key(name_str_id) references localization(id),
description_str_id varchar(500),
cp_type_id int,
foreign key(cp_type_id) references dim_cp_type(id),
cp_category_group_id int,
foreign key(cp_category_group_id) references cp_category_group(id),
repeat_type_id int,
foreign key(repeat_type_id) references dim_repeat_type(id),
cp_repeat_reminder_id int,
is_template boolean,
is_top_template boolean,
db_create_dts date,
db_create_source nvarchar(320),
db_updated_dts date,
db_update_source nvarchar(320)
);
我错过了什么?为什么所提交的文件甚至不存在于导致此问题的相关实体中?
您是否尝试过通过SQL查询为其分配默认值? 如果您已经有一个数据库 GUI(如 MySQL workbench),您只需执行此附加查询即可将字段设置为具有默认值。
ALTER TABLE `checking_point` ALTER `cp_type_id` SET DEFAULT NULL
或者您可以更改您已有的sql查询
create table checking_point(
id int primary key NOT NULL auto_increment,
name_str_id int,
foreign key(name_str_id) references localization(id),
description_str_id varchar(500),
cp_type_id int DEFAULT NULL, <-HERE
foreign key(cp_type_id) references dim_cp_type(id),
cp_category_group_id int,
foreign key(cp_category_group_id) references cp_category_group(id),
repeat_type_id int,
foreign key(repeat_type_id) references dim_repeat_type(id),
cp_repeat_reminder_id int,
is_template boolean,
is_top_template boolean,
db_create_dts date,
db_create_source nvarchar(320),
db_updated_dts date,
db_update_source nvarchar(320)
);
通过分配 te 默认值,您将不再为 cp_type_id 获取这些类型的异常,同样的方法适用于它抱怨的任何其他字段。