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

Neil picture Neil · Dec 16, 2015 · Viewed 21.9k times · Source

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?

Answer

OPK picture OPK · Dec 16, 2015

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.