现在的位置: 首页 > 综合 > 正文

利用JPA注解插入数据进数据库时报detached entity passed to persist的解决办法

2018年05月03日 ⁄ 综合 ⁄ 共 287字 ⁄ 字号 评论关闭

在用到用JPA或者Hibernate注解的过程中,有时我们进行增删改查的操作时,会报detached entity passed to persist错误,这个原因就是主键生成有问题。

@Id
@Column(name="uuid")
@GenericGenerator(name = "idGenerator", strategy = "uuid")
@GeneratedValue(generator="idGenerator") //使用uuid的生成策略  
private String id;

这样设置就会报错,因为默认的id为null,而数据库中的主键不能为null,只需改变一下id = " ",即可。

抱歉!评论已关闭.