user1482800
user1482800

Reputation: 83

Hibernate creates new row on save and not updates

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

Answers (2)

Vignesh Shiv
Vignesh Shiv

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

Viswanath Donthi
Viswanath Donthi

Reputation: 1821

You need to use saveOrUpdate() session method as:

sessionFactory.getCurrentSession().saveOrUpdate(configTable);

Upvotes: 2

Related Questions