Spring Data JPA
Methods
By Ramesh Fadatare (Java Guides)
save() method
As the name depicts, the save() method allows
us to save an entity to the DB.
Saving an entity can be performed with the
CrudRepository.save(…) method. It persists or
merges the given entity by using the
underlying JPA EntityManager. If the entity
has not yet been persisted, Spring Data JPA
saves the entity with a call to the
entityManager.persist(…) method. Otherwise,
it calls the entityManager.merge(…) method.
Update existing entity using save() method
As the name depicts, the save() method allows
us to save an entity to the DB.
Saving an entity can be performed with the
CrudRepository.save(…) method. It persists or
merges the given entity by using the
underlying JPA EntityManager. If the entity
has not yet been persisted, Spring Data JPA
saves the entity with a call to the
entityManager.persist(…) method. Otherwise,
it calls the entityManager.merge(…) method.
saveAll() method
As the name depicts, the saveAll() method
allows us to save multiple entities to the DB
It belongs to the CrudRepository interface
de ned by Spring Data.
saveAll() method returns a list of Iterable
objects
fi
.
ndById() method
As the name depicts, the ndById() method
allows us to get or retrieve an entity based on
a given id (primary key) from the DB.
It belongs to the CrudRepository interface
de ned by Spring Data.
ndById() method returns Optional of type
Entity
fi
fi
fi
fi
ndAll() method
As the name depicts, the ndAll() method
allows us to get or retrieve all the entities
from the database table.
It belongs to the CrudRepository interface
de ned by Spring Data.
ndById() method returns List of Iterable
objects
fi
fi
fi
fi
deleteById() method
As the name depicts, the deleteById() method
allows us to delete an entity by id from the
database table.
It belongs to the CrudRepository interface
de ned by Spring Data.
deleteById() method returns void (nothing)
fi
delete() method
As the name depicts, the delete() method
allows us to delete an entity from the database
table.
It belongs to the CrudRepository interface
de ned by Spring Data.
delete() method returns void (nothing)
fi
deleteAll() two
overloaded method
As the name depicts, the deleteAll() method
allows us to delete all the entities from the
database table.
It belongs to the CrudRepository interface
de ned by Spring Data.
deleteAll() method returns void (nothing)
fi
existsById() method
As the name depicts, the existsById() method
allows us to check if the entity exists with a
given id in a database table.
It belongs to the CrudRepository interface
de ned by Spring Data.
existsById() method returns boolean (true
or false)
fi
count() method
As the name depicts, the count() method
allows us to count the number of records that
exist in a database table.
It belongs to the CrudRepository interface
de ned by Spring Data.
count() method returns long (numbers of
records)
fi