Gilo
Gilo

Reputation: 720

Hibernate list() fail after saveOrUpdate

When I run the following code:

MyObject myObject = ...
session.saveOrUpdate(myObject)
List<MyObject> list = session.createCriteria(MyObject.class).list()

I get the following exception:

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

If I execute .list() before .saveOrUpdate, there is no exception. Any idea why does an exception is thrown in that flow, and how can I solve it?

Thanks

Upvotes: 1

Views: 156

Answers (1)

Fabricio Lemos
Fabricio Lemos

Reputation: 2955

It's hard to answer it without the mapping of your entity and without the code you use to instantiate it.

This error can happen if you manually set the object id when the mapping says it should be auto-generated.

The error only happens when you call .list() because that's probably the moment Hibernate flushes the saveOrUpdate operation. Again, it's hard to tell without the rest of the code.

Upvotes: 1

Related Questions