I am trying to insert or update a db record with the following code:
Category category = new Category();
category.setName('catName');
category.setId(1L);
categoryDao.saveOrUpdate(category);
When there is a category with id = 1 in the database, everything works. But if there is no entry with id = 1, I got the following exception:
org.hibernate.StaleStateException:
Batch update returned unexpected row count from update [0]; actual row count: 0;
expected: 1:
Here are my class settings, getters and class constructors that are clear:
@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 request for sleep mode:
update
Category
set
name=?,
parent_id=?
where
id=?
So it looks like hibernates tryis is updating the record, not inserting a new one. What am I doing wrong here?
source
share