"ids for this class must be manually assigned before calling save()" 带有字符串 ID
"ids for this class must be manually assigned before calling save()" with string id
我在内存测试中写休眠时出现异常。
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():
@Entity
public Account{
@Id
String num;
}
首先,这个 pojo 的 id 是字符串,我无法更改它,我没有访问 DB 和更改 pojo 的权限,我所能做的就是创建内存中测试。
然后,在我保存这个pojo之前,我填写了这个pojo中的每个字段,仍然抛出这个异常。而且因为内存测试,内存DB是空的,没有id冲突
知道吗,还有什么可以导致此异常?
您需要在变量顶部添加 @GeneratedValue
。如果不这样做,则需要给 id 一个值。内存测试与否无关紧要。
我在内存测试中写休眠时出现异常。
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():
@Entity
public Account{
@Id
String num;
}
首先,这个 pojo 的 id 是字符串,我无法更改它,我没有访问 DB 和更改 pojo 的权限,我所能做的就是创建内存中测试。
然后,在我保存这个pojo之前,我填写了这个pojo中的每个字段,仍然抛出这个异常。而且因为内存测试,内存DB是空的,没有id冲突
知道吗,还有什么可以导致此异常?
您需要在变量顶部添加 @GeneratedValue
。如果不这样做,则需要给 id 一个值。内存测试与否无关紧要。