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>
entityClass
The type of class which implementations of this class is to operate on.protected javax.persistence.EntityManager
entityManager
protected org.slf4j.Logger
logger
-
Constructor Summary
Constructors Constructor Description AbstractRepository(javax.persistence.EntityManager entityManager)
Constructor to injectEntityManager
dependency. -
Method Summary
Modifier and Type Method Description void
delete(T entity)
Deletes the given entity.void
deleteById(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 void
setEntityClass(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 injectEntityManager
dependency.- 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:
save
in interfaceRepository<T,ID>
- Parameters:
entity
- not null- Returns:
- the saved entity
-
update
Updates a given entity and returns the updated instance.- Specified by:
update
in interfaceRepository<T,ID>
- Parameters:
entity
- not null- Returns:
- the updates entity
-
findAll
Retrieves all instances of the class type.- Specified by:
findAll
in interfaceRepository<T,ID>
- Returns:
- all entities
-
findById
Retrieves an entity with the given id.- Specified by:
findById
in 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:
deleteById
in interfaceRepository<T,ID>
- Parameters:
id
- not null
-
delete
Deletes the given entity.- Specified by:
delete
in interfaceRepository<T,ID>
- Parameters:
entity
- not null
-