Insert a new row using JpaRepository in java

The method .save(Entity entity), inherited from the Parent interface CrudRepository can be used to both update an existing entity or create a new one.


Your repository interface extends from JpaRepository which extends from CrudRepository. save and saveAndFlush methods can be used as default to insert new rows.

save: Inserts new rows. You can give row list as parameter to this method. In your example it should be used like this:

testRepo.save(testEntity);

or

testRepo.save(testEntities);

saveAndFlush: Inserts only a new row and commit the changes immediately. In your example it should be used like this:

testRepo.saveAndFlush(testEntity);