Reputation: 83
I am using MySQL + spring + hibernate.
When I execute the following code it creates a new row:
sessionFactory.getCurrentSession()
.save(configTable);
However this code below updates
sessionFactory.getCurrentSession()
.update(configTable);
I am not sure why the above code creates a new row in tables it should update in both cases to my understanding, Any idea what could I be missing? Or what info will you need to help me track the problem...
Upvotes: 1
Views: 3521
Reputation: 1157
When update pass Entity primary key for update.
Make sure that your Entity have this annotation for save and update.
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
Upvotes: 1
Reputation: 1821
You need to use saveOrUpdate() session method as:
sessionFactory.getCurrentSession().saveOrUpdate(configTable);
Upvotes: 2