Hibernate saveOrUpdate fails when I execute it on empty table.

Posted by Vladimir on Stack Overflow See other posts from Stack Overflow or by Vladimir
Published on 2009-11-25T11:07:30Z Indexed on 2010/06/14 10:32 UTC
Read the original article Hit count: 172

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about update