日期:2014-05-17  浏览次数:20623 次

ssh关于主键生成策略问题
entity实体类如下
        @Id
@GeneratedValue(generator="UserInfoId")
@GenericGenerator(name="UserInfoId", strategy="assigned")
@Column(name="id",unique=true,nullable=false)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
就是用到注解,在执行插入操作的时候就出错了org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
控制台执行的是更新方法update,可是我要执行的是插入啊,我就想是不是主键生成策略有问题还是怎么的,第一次用注解的方式不知道是不是什么地方还要配置还是写漏了,我这个ssh项目在没有写成基于全注解之前是没有问题的
ssh 注解

------解决方案--------------------
你好,希望能帮到你。
1.首先看看你用的是什么数据库,如果是mysql之类的话要在表中设置自动增长的,如果是oracle要建序列的。
2.然后在你的实体类中的主键id上面要指定生成策略的,如果是mysql的话,改成如下的方式试一试@GeneratedValue(strategy = IDENTITY)
------解决方案--------------------
总算解决了,去掉@GeneratedValue(generator="UserInfoId")
 @GenericGenerator(name="UserInfoId", strategy="assigned")这两行就可以了,hibernate可能默认是assigned