hibernate 异常a different object with the same identifier value was already associated with the session

在使用hibernate的时候发现了一个问题,记录一下解决方案。

前提开启了事务和事务间并无commit,进行两次save,第二次的时候爆出下面的异常a different object with the same identifier value was already associated with the session

翻译一下session中已经存在了拥有该标识的对象,不能新建另外一个了。

这里的identifier(标识)指的就是表的主键,而后发现主键是自增的,但是并没有在Entity中增加自增配置

@Id主键自增的方法

   @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

猜你喜欢

转载自www.cnblogs.com/tianmh/p/9254924.html