在使用hibernate项目中,持久化类的ID用Long与long有很大区别!!
用long 作为id,当一个新的临时类要持久化时会报一个这种错:
Exception executing batch: org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
因为这个临时类,已经有一个id=0 (long的默认值);这时hibernate 为错认为这是一条持久化类,从而进行更新操作,故报错!!
这时把持久类的id 类型改成Long 就OK了,再create 时,临时类的id为null ,hibernate 才会正确的判断这是临时类而进行save操作!!
用long 作为id,当一个新的临时类要持久化时会报一个这种错:
Exception executing batch: org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
因为这个临时类,已经有一个id=0 (long的默认值);这时hibernate 为错认为这是一条持久化类,从而进行更新操作,故报错!!
这时把持久类的id 类型改成Long 就OK了,再create 时,临时类的id为null ,hibernate 才会正确的判断这是临时类而进行save操作!!
本文探讨了在Hibernate项目中,持久化类的ID使用Long与long的区别。详细解释了为何使用long作为ID会在创建新的临时对象时导致StaleStateException异常,并说明了如何通过将ID类型改为Long来解决该问题。
67

被折叠的 条评论
为什么被折叠?



