Package NTNU.IDATT1002.repository
Class AbstractRepository<T,ID>
java.lang.Object
NTNU.IDATT1002.repository.AbstractRepository<T,ID>
- Type Parameters:
T- type of entityID- type of entity id
- All Implemented Interfaces:
Repository<T,ID>
- Direct Known Subclasses:
GeoLocatioRepository,LoginRepository,MetadataRepository,PagingAndSortingRepository,TagRepository,UserRepository
public abstract class AbstractRepository<T,ID> extends java.lang.Object implements Repository<T,ID>
Abstract Entity Repository Abstract Class.
Implements regular Create, Read, Update and Delete operations defined in
Repository.
This class can be easily extended to support type specific operations through concrete implementations.- Version:
- 1.1 03.04.20
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.Class<T>entityClassThe type of class which implementations of this class is to operate on.protected javax.persistence.EntityManagerentityManagerprotected org.slf4j.Loggerlogger -
Constructor Summary
Constructors Constructor Description AbstractRepository(javax.persistence.EntityManager entityManager)Constructor to injectEntityManagerdependency. -
Method Summary
Modifier and Type Method Description voiddelete(T entity)Deletes the given entity.voiddeleteById(ID id)Deletes an entity with the given id.java.util.List<T>findAll()Retrieves all instances of the class type.java.util.Optional<T>findById(ID id)Retrieves an entity with the given id.java.util.Optional<T>save(T entity)Saves a given entity and returns the saved instance.protected voidsetEntityClass(java.lang.Class<T> classTypeToSet)Set the type of class which this class is to operate on.java.util.Optional<T>update(T entity)Updates a given entity and returns the updated instance.
-
Field Details
-
entityClass
The type of class which implementations of this class is to operate on. -
entityManager
protected javax.persistence.EntityManager entityManager -
logger
protected org.slf4j.Logger logger
-
-
Constructor Details
-
AbstractRepository
public AbstractRepository(javax.persistence.EntityManager entityManager)Constructor to injectEntityManagerdependency.- Parameters:
entityManager- the entity manager to utilize
-
-
Method Details
-
setEntityClass
Set the type of class which this class is to operate on.- Parameters:
classTypeToSet- the type of class
-
save
Saves a given entity and returns the saved instance.- Specified by:
savein interfaceRepository<T,ID>- Parameters:
entity- not null- Returns:
- the saved entity
-
update
Updates a given entity and returns the updated instance.- Specified by:
updatein interfaceRepository<T,ID>- Parameters:
entity- not null- Returns:
- the updates entity
-
findAll
Retrieves all instances of the class type.- Specified by:
findAllin interfaceRepository<T,ID>- Returns:
- all entities
-
findById
Retrieves an entity with the given id.- Specified by:
findByIdin interfaceRepository<T,ID>- Parameters:
id- not null- Returns:
- the entity with the given id if found, else Optional.empty()
-
deleteById
Deletes an entity with the given id.- Specified by:
deleteByIdin interfaceRepository<T,ID>- Parameters:
id- not null
-
delete
Deletes the given entity.- Specified by:
deletein interfaceRepository<T,ID>- Parameters:
entity- not null
-