T - the entity type that we want to build a repository forK - the key type of the entitypublic abstract class JpaRepository<T,K extends java.io.Serializable> extends java.lang.Object implements Repository<T,K>, IterableRepository<T,K>, DeleteableRepository<T,K>, java.lang.AutoCloseable
| Modifier and Type | Field and Description | 
|---|---|
| protected static int | DEFAULT_PAGE_SIZE | 
| protected static java.util.logging.Logger | LOGGER | 
| Constructor and Description | 
|---|
| JpaRepository() | 
| Modifier and Type | Method and Description | 
|---|---|
| boolean | add(T entity)adds and commits a new entity to the persistence store | 
| java.util.List<T> | all()gets all entities from the repository. | 
| void | close() | 
| T | create(T entity)adds a new entity to the persistence store | 
| void | delete(T entity)removes the object from the persistence storage. | 
| boolean | deleteById(K entityId)Removes the entity with the specified ID from the repository. | 
| protected javax.persistence.EntityManager | entityManager() | 
| protected javax.persistence.EntityManagerFactory | entityManagerFactory() | 
| java.util.Optional<T> | findById(K id)reads an entity given its K | 
| java.util.Optional<T> | first() | 
| java.util.List<T> | first(int n)returns the first n entities according to its "natural" order | 
| java.util.Iterator<T> | iterator() | 
| java.util.Iterator<T> | iterator(int pagesize)returns a paged iterator | 
| T | last() | 
| java.util.List<T> | page(int pageNumber,
    int pageSize) | 
| java.util.List<T> | pageWithFiltering(javax.persistence.criteria.CriteriaQuery<T> query,
                 int pageNumber) | 
| protected abstract java.lang.String | persistenceUnitName()Derived classes should implement this method to return the name of the
 persistence unit | 
| T | read(K id)reads an entity given its K | 
| T | save(T entity)Inserts or updates an entity and commits. | 
| long | size()returns the number of entities in the persistence store | 
| T | update(T entity) | 
protected static final int DEFAULT_PAGE_SIZE
protected static final java.util.logging.Logger LOGGER
protected javax.persistence.EntityManagerFactory entityManagerFactory()
protected javax.persistence.EntityManager entityManager()
public T create(T entity)
entity - public java.util.Optional<T> findById(K id)
findById in interface Repository<T,K extends java.io.Serializable>id - public void delete(T entity)
delete in interface DeleteableRepository<T,K extends java.io.Serializable>delete in interface Repository<T,K extends java.io.Serializable>entity - public boolean deleteById(K entityId)
deleteById in interface DeleteableRepository<T,K extends java.io.Serializable>entityId - if the delete operation makes no sense for this repositorypublic long size()
size in interface Repository<T,K extends java.io.Serializable>public boolean add(T entity)
It is controversial if the repository class should have explicit knowledge of when to start a transaction and end it as well as to know when to open a connection and close it. this is the kind of stuff that the container (e.g., web server) should handle declaratively
the following methods open and commit a transaction: add() save() replace() remove()
note that other methods in this class just work with the JPA unit of work and expect the container to begin/commit transactions. they are: create() update() delete()
add in interface Repository<T,K extends java.io.Serializable>entity - public void close()
           throws java.lang.Exception
close in interface java.lang.AutoCloseablejava.lang.Exceptionpublic T save(T entity)
note that you should reference the return value to use the persisted entity, as the original object passed as argument might be copied to a new object
check JPA implementation patterns for a discussion on saveOrUpdate() behavior and merge()
save in interface Repository<T,K extends java.io.Serializable>entity - public java.util.List<T> first(int n)
n - public java.util.Optional<T> first()
public T last()
public java.util.List<T> page(int pageNumber, int pageSize)
public java.util.Iterator<T> iterator(int pagesize)
iterator in interface IterableRepository<T,K extends java.io.Serializable>pagesize - public java.util.List<T> pageWithFiltering(javax.persistence.criteria.CriteriaQuery<T> query, int pageNumber)
public java.util.Iterator<T> iterator()
iterator in interface java.lang.Iterable<T>public java.util.List<T> all()
Repositoryall in interface Repository<T,K extends java.io.Serializable>protected abstract java.lang.String persistenceUnitName()