Class AbstractRepository<T,​ID>

java.lang.Object
NTNU.IDATT1002.repository.AbstractRepository<T,​ID>
Type Parameters:
T - type of entity
ID - 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 inject EntityManager 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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • entityClass

      protected java.lang.Class<T> 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 inject EntityManager dependency.
      Parameters:
      entityManager - the entity manager to utilize
  • Method Details

    • setEntityClass

      protected void setEntityClass​(java.lang.Class<T> classTypeToSet)
      Set the type of class which this class is to operate on.
      Parameters:
      classTypeToSet - the type of class
    • save

      public java.util.Optional<T> save​(T entity)
      Saves a given entity and returns the saved instance.
      Specified by:
      save in interface Repository<T,​ID>
      Parameters:
      entity - not null
      Returns:
      the saved entity
    • update

      public java.util.Optional<T> update​(T entity)
      Updates a given entity and returns the updated instance.
      Specified by:
      update in interface Repository<T,​ID>
      Parameters:
      entity - not null
      Returns:
      the updates entity
    • findAll

      public java.util.List<T> findAll()
      Retrieves all instances of the class type.
      Specified by:
      findAll in interface Repository<T,​ID>
      Returns:
      all entities
    • findById

      public java.util.Optional<T> findById​(ID id)
      Retrieves an entity with the given id.
      Specified by:
      findById in interface Repository<T,​ID>
      Parameters:
      id - not null
      Returns:
      the entity with the given id if found, else Optional.empty()
    • deleteById

      public void deleteById​(ID id)
      Deletes an entity with the given id.
      Specified by:
      deleteById in interface Repository<T,​ID>
      Parameters:
      id - not null
    • delete

      public void delete​(T entity)
      Deletes the given entity.
      Specified by:
      delete in interface Repository<T,​ID>
      Parameters:
      entity - not null