org.hibernate.AssertionFailure: null id in xxx entry (don't flush the Session after an exception occurs)

时间:2023-03-09 06:48:53
org.hibernate.AssertionFailure: null id in xxx entry (don't flush the Session after an exception occurs)

  网上找了很久,发现造成原因有很多种,后来终于发现了端倪;看提示是发生了异常,查看业务代码,发现有这个逻辑:先插入记录,如果有唯一键约束异常(并发造成),catch时查询已存在的记录,查询的时候就报了此错误。

  原因:异常后,再次查询的时候触发了autoFlush,先前保存失败的记录会再次保存,导致抛出如标题所述异常。

  解决方法:数据库是mysql,改为采用 insert ignore into语句进行插入

  当然有很多处理方法,就不赘述了