今天遇到了这个错误
错误原因是:在实体类中增加了序列自增
@Id
@SequenceGenerator(name="sequenceGenerator", allocationSize = 1, initialValue = 1, sequenceName = "SEQ_BUSINESS_SORT_VOTE")
@GeneratedValue(generator="sequenceGenerator", strategy = GenerationType.SEQUENCE)
@Column(name="ID")
public Long getId() {
return id;
}
而代码中JPA在执行persist前已经设置了主键ID
Long id = setIdByEntity(entity); 此处为设置主键
if(null == id){
return null;
}
entityManager.persist(entity);
调用persist方法时又调用了序列的主键自增 所以引起这个错误
// @SequenceGenerator(name="sequenceGenerator", allocationSize = 1, initialValue = 1, sequenceName = "SEQ_BUSINESS_SORT_VOTE")
// @GeneratedValue(generator="sequenceGenerator", strategy = GenerationType.SEQUENCE)
只要把这两行代码注释掉就OK了