Home > Back-end >  JPA save() not updating existing rows
JPA save() not updating existing rows

Time:06-07

What's the problem?

We use the static ids for primary keys, We don't want auto-increment value as a primary key.

We use static id's to save the data into database. First time it's working I’m fine, all data inserted into database. Now we are trying to update the data with existing id, got the following error

could not execute statement; SQL [n/a]; constraint [tbl_lookup.PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

My JPA code:

@Entity
@Table(schema = "MyApp", name = "TBL_lookup")
public class JpaLookup implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(unique = true,insertable = false,updatable = false, nullable = false)
private Integer id;

 //other columns here
}

Sample data:

  id      name   
--------------------------
  100     CityName  
  101     CountryName

CodePudding user response:

Problem seems like you have set updatable as false, which we generally do when we are using composite key.

try using updatable = true

CodePudding user response:

How are you updating? Did you try retrieving the record first by using "findById" method , then updating the entity object and then calling save() method?

  • Related