且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么在删除实体时出现StaleObjectStateException?

更新时间:2023-12-05 15:05:22

在PaulNUK的提示下,我向我的实体添加了一个带@Version注释的字段.现在,在将实体添加到数据库中时,我收到以下异常:org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1.

Following the hint of PaulNUK, I added an @Version annotated field to my entity. Now I received the following exception on adding an entity to my database: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1.

原来是问题所在:我的实体有一个UUID作为ID,其注释如下:

The problem turned out to be the following: My entity has a UUID as an id, which was annotated as follows:

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")

添加

@Column(columnDefinition = "BINARY(16)")

解决了问题.