I am using spring-data and hibernate for creating tables and inserting Data. From different threads i found that save method from JPARepository (From CRUDRepository) will only update the record if the record already exists. Below is one of the thread i found some information:
https://stackoverflow.com/a/40608937/10356053
I have an Entity which is duplicating the records (jpa is considering as a new insert even if the Object is same). I am not really sure whats happening or a Solution for this issue. Any suggestions are appreciated. Below is the Entity i have created:
CustomerEntity.java:
@Getter
@Setter
@Table(name = "Customers")
@Entity
public class CustomersEntity extends BasicEntity {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "CustomerId", updatable = false, nullable = false)
private UUID customerid;
//and some other fields
}
BasicEntity.java:
@Getter
@Setter
public class BasicEntity implements serializable {
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", updatable = false)
@CreatedDate
private Date createdAt;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at")
@LastModifiedDate
private Date updatedAt;
}
Repository.java
@Repository
public interface Repositorty extends JpaRepository<CustomersEntity, UUID> {
}
When we are trying to save the above entites:
repository.save(customerEntity)
will persist as a new record in the database. Any suggestions are helpful. I am thinking is this because of the createdTime and updatedTime from the parent class? If yes, how do we avoid this? TIA.