null id in com.rocky.** entry 错误处理

时间:2023-03-08 20:57:02

1. 概述

使用hibernate往mysql数据库插入记录出错如下

10:37:57,364 ERROR [AssertionFailure] an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: null id in com.rocky.console.domain.Downinfo entry (don't flush the Session after an exception occurs)

2. 分析

对比:

Downinfo对象中id是long类型 ————数据库表downinfo中id字段bigint类型,

<generator class="native"/>————数据库中设置了自增

看来并没有错

代码断点发现 与表中非空字段对应的bean属性为null,而表中又没有设置默认值(如""或0之类),

这是出错的原因 只不过hibernate提示不准确

3.解决方案

将数据库表中字段not null 部分设置默认值或者 在bean中对相应属性做默认赋值。

done