0% found this document useful (0 votes)
54 views

Important Spring Data JPA Methods

The document discusses various methods provided by Spring Data JPA for performing CRUD operations. These include save(), saveAll(), findById(), findAll(), deleteById(), delete(), deleteAll(), existsById(), and count(). The save() method allows saving an entity to the database, while saveAll() saves multiple entities. findById() and findAll() retrieve entities by ID or all entities. deleteById(), delete(), and deleteAll() remove entities by ID, single entity, or all entities. existsById() checks if an entity exists by ID and count() returns the number of records. All methods are part of the CrudRepository interface defined by Spring Data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Important Spring Data JPA Methods

The document discusses various methods provided by Spring Data JPA for performing CRUD operations. These include save(), saveAll(), findById(), findAll(), deleteById(), delete(), deleteAll(), existsById(), and count(). The save() method allows saving an entity to the database, while saveAll() saves multiple entities. findById() and findAll() retrieve entities by ID or all entities. deleteById(), delete(), and deleteAll() remove entities by ID, single entity, or all entities. existsById() checks if an entity exists by ID and count() returns the number of records. All methods are part of the CrudRepository interface defined by Spring Data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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

You might also like