Neil
Neil

Reputation: 3036

"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?

Upvotes: 10

Views: 28017

Answers (1)

OPK
OPK

Reputation: 4180

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.

Upvotes: 17

Related Questions