Hibernate saveOrUpdate fails when I execute it on empty table.
- by Vladimir
I'm try to insert or update db record with following code:
Category category = new Category();
category.setName('catName');
category.setId(1L);
categoryDao.saveOrUpdate(category);
When there is a category with id=1 already in database everything works. But if there is no
record with id=1 I got following exception:
org.hibernate.StaleStateException:
Batch update returned unexpected row count from update [0]; actual row count: 0;
expected: 1:
Here is my Category class setters, getters and constructors ommited for clarity:
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne
private Category parent;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
private List<Category> categories = new ArrayList<Category>();
}
In the console I see this hibernate query:
update
Category
set
name=?,
parent_id=?
where
id=?
So looks like hibernates tryis to update record instead of inserting new. What am I doing wrong here?