Mahdi
Mahdi

Reputation: 2289

Spring JPA: How to check whether "save" function created or updated the data?

Let's say I have this:

mySpringJpaRepository.save(myEntity);

How can I check whether "save" call created a new row in the DB or updated an existing one? (Without making extra DB calls)

Upvotes: 3

Views: 1111

Answers (1)

Geeth
Geeth

Reputation: 554

If your entity has a primary key that is the Identity of the entity. That entity identity tests whether two persistent objects represent the new state or existing state in the datastore.

If that Identity value is null , that is in new state, when called Save() function crated new row.

If that Identity has a value , that is in exist state, when called Save() function update existing row.

@Id used to represent Identity of entity(Primary Key)

@Id
private String code;

@Id
private Long id;

Upvotes: 1

Related Questions