Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ids for this class must be manually assigned before calling save()" with string id

I got exception when I'm writing hibernate in memory test.

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():

@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.

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.

Any idea, what else can cause this exception?

like image 351
Neil Avatar asked Dec 16 '15 16:12

Neil


Video Answer


1 Answers

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.

like image 167
OPK Avatar answered Oct 15 '22 01:10

OPK