java “必须在使用字符串 id 调用 save() 之前手动分配此类的 id”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34317503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"ids for this class must be manually assigned before calling save()" with string id
提问by Neil
I got exception when I'm writing hibernate in memory test.
当我在内存测试中编写 hibernate 时出现异常。
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():
org.hibernate.id.IdentifierGenerationException:必须在调用 save() 之前手动分配此类的 ID:
@Entity
public Account{
@Id
String num;
}
First, this id of pojo is String and I cannot change it, I don't have access to DB and change pojo, all I can do is create in-memory tests.
首先,pojo 的这个 id 是 String 并且我无法更改它,我无权访问 DB 并更改 pojo,我所能做的就是创建内存中测试。
Then, before I save this pojo, I filled every field in this pojo, still it throws this exception. And because of in-memory test, the in memory DB is empty, there're not conflict id there.
然后,在我保存这个 pojo 之前,我填写了这个 pojo 中的每个字段,它仍然抛出这个异常。而且由于内存测试,内存数据库是空的,那里没有冲突ID。
Any idea, what else can cause this exception?
任何想法,还有什么可能导致此异常?
回答by OPK
You need to add @GeneratedValue
on top of the variable. If you don't, you need to give the id a value. Doesn't matter its in memory test or not.
您需要@GeneratedValue
在变量之上添加。如果不这样做,则需要为 id 指定一个值。它在内存测试中与否无关紧要。