Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. public interface IDao<K, E> {
  2.  
  3.     /**
  4.      * Performs SQL insert for the specified entity
  5.      * @param entity domain object to store in database
  6.      */
  7.     void persist(E entity);
  8.  
  9.     /**
  10.      * Performs SQL DELETE based on entity PK
  11.      * @param id domain object to remove from database
  12.      */
  13.     void remove(K id);
  14.  
  15.     /**
  16.      * Performs SQL SELECT with WHERE clause based in PK
  17.      * @param id primary key attribute of entity
  18.      * @return entity of type E with id of value of K
  19.      */
  20.     E findById(K id);
  21.    
  22.     /**
  23.      * Performs SQL UPDATE for the specified entity
  24.      * @param entity domain object to store in database
  25.      */
  26.     void update(E entity);
  27. }
  28.  
  29. public class LicenseDao implements IDao<Integer, License> {
  30.  
  31.     private EntityManagerFactory entityManagerFactory;
  32.     private EntityManager entityManager;
  33.     private Logger logger = LoggerFactory.getLogger(LicenseDao.class);
  34.  
  35.     /**
  36.      * JPA EntityManagerFactory instance injected, All other new parameters can should be injected similarly
  37.      * to ensure compatibility with route controllers if extending this class.
  38.      *
  39.      * @param entityManagerFactory Injected by Jooby Framework IoC DI module
  40.      */
  41.     @Inject
  42.     public LicenseDao(@Named("testPU") EntityManagerFactory entityManagerFactory) {
  43.         this.entityManagerFactory = entityManagerFactory;
  44.         this.entityManager = entityManagerFactory.createEntityManager();
  45.     }
  46.  
  47.     /**
  48.      * Performs SQL insert for the specified entity
  49.      * @param entity domain object to store in database
  50.      */
  51.     @Override
  52.     public void persist(License entity) {
  53.         entityManager.getTransaction().begin();
  54.         entityManager.persist(entity);
  55.         entityManager.getTransaction().commit();
  56.         entityManager.close();
  57.     }
  58.  
  59.     /**
  60.      * Performs SQL DELETE based on entity PK
  61.      * @param id domain object to remove from database
  62.      */
  63.     @Override
  64.     public void remove(Integer id) {
  65.         entityManager.getTransaction().begin();
  66.         License license = entityManager.find(License.class, id);
  67.         if (license == null) {
  68.             //Add notfication of failure
  69.             logger.warn("Trying to remove non-exiting licence: " + id);
  70.         } else {
  71.             logger.info("Removing " + license);
  72.             entityManager.remove(license);
  73.         }
  74.         entityManager.getTransaction().commit();
  75.         entityManager.close();
  76.  
  77.     }
  78.  
  79.     /**
  80.      * Performs SQL SELECT with WHERE clause based in PK
  81.      * @param id primary key attribute of entity
  82.      * @return entity of type E with id of value of K
  83.      */
  84.     @Override
  85.     public License findById(Integer id) {
  86.         return entityManager.find(License.class, id);
  87.     }
  88.  
  89.     /**
  90.      * Find all Licenses related to an organisation. Note that this will return all Licenses
  91.      * and may be slow if large amount of Licenses exist.
  92.      * @param organisationId organisation primary key used to relate conductor to organisation
  93.      * @return List of Licenses
  94.      */
  95.     public List<License> findAllByOrganisationId(Integer organisationId) {
  96.         EntityManager entityManager = entityManagerFactory.createEntityManager();
  97.  
  98.         return entityManager.
  99.                 createQuery(
  100.                         "SELECT l from License l " +
  101.                                 "WHERE l.organisation.organisationId = :organisationId", License.class)
  102.                 .setParameter("organisationId", organisationId)
  103.                 .getResultList();
  104.     }
  105.    
  106.     /**
  107.      * Updates the license
  108.      * @param license domain object to store in database
  109.      */
  110.     @Override
  111.     public void update(License license) {
  112.         entityManager.getTransaction().begin();
  113.         entityManager.merge(license);
  114.         entityManager.getTransaction().commit();
  115.         entityManager.close();
  116.     }
  117.    
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement