SHARE
TWEET

Untitled

a guest Dec 8th, 2019 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package pl.polsl.zti.db1.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.persistence.EntityManager;
  6. import javax.persistence.EntityManagerFactory;
  7. import javax.persistence.EntityTransaction;
  8. import javax.persistence.Persistence;
  9. import javax.persistence.Query;
  10.  
  11. import pl.polsl.zti.db1.ConfigConsts;
  12. import pl.polsl.zti.db1.domain.Client;
  13.  
  14. public class ClientDaoImplJpa implements ClientDao {
  15.  
  16.     private static final EntityManagerFactory ENTITY_MANAGER_FACTORY;
  17.  
  18.     static {
  19.         ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory(ConfigConsts.PERSISTANCE_UNIT_NAME);
  20.     }
  21.  
  22.     @Override
  23.     public Client getClient(int id) {
  24.         final EntityManager entityManager = ENTITY_MANAGER_FACTORY.createEntityManager();
  25.         final Client theClient = entityManager.find(Client.class, id);
  26.         entityManager.close();
  27.         return theClient;
  28.     }
  29.  
  30.     @Override
  31.     public List<Client> getClients() {
  32.         final EntityManager entityManager = ENTITY_MANAGER_FACTORY.createEntityManager();
  33.         final Query query = entityManager.createQuery("select c from Client as c", Client.class);
  34.         // działa w Hibernate, ale nie w EclipseLink:
  35. //        final Query query = entityManager.createQuery("from Client", Client.class);
  36.         final List<Client> clientList = query.getResultList();
  37.         entityManager.close();
  38.         return clientList;
  39.     }
  40.  
  41.     @Override
  42.     public List<Client> getClients(String name) {
  43.         if (name == null || name.trim().isEmpty()) {
  44.             return getClients();
  45.         }
  46.  
  47.         final EntityManager entityManager = ENTITY_MANAGER_FACTORY.createEntityManager();
  48.         final Query query = entityManager.createQuery("select c from Client as c where c.name like :parame", Client.class);
  49.         query.setParameter("parame", name);
  50.         final List<Client> clientList = query.getResultList();
  51.         entityManager.close();
  52.         return clientList;
  53.     }
  54.  
  55.     @Override
  56.     public void insertClients(List<Client> clients) {
  57.         if (clients == null || clients.isEmpty()) {
  58.             return;
  59.         }
  60.  
  61.         final EntityManager entityManager = ENTITY_MANAGER_FACTORY.createEntityManager();
  62.         final EntityTransaction transaction = entityManager.getTransaction();
  63.         transaction.begin();
  64.        
  65.         for (Client cln : clients){
  66.             entityManager.persist(cln);
  67.         }
  68.        
  69.         transaction.commit();
  70.         entityManager.close();
  71.     }
  72.  
  73.     @Override
  74.     public void updateClient(Client client) {
  75.        
  76.         if (client == null) {
  77.             return;
  78.         }
  79.  
  80.         final EntityManager entityManager = ENTITY_MANAGER_FACTORY.createEntityManager();
  81.         final EntityTransaction transaction = entityManager.getTransaction();
  82.         transaction.begin();
  83.        
  84.         entityManager.merge(client);
  85.        
  86.         transaction.commit();
  87.         entityManager.close();
  88.     }
  89.  
  90.     @Override
  91.     public void deleteClient(Client client) {
  92.         final EntityManager entityManager = ENTITY_MANAGER_FACTORY.createEntityManager();
  93.         final EntityTransaction transaction = entityManager.getTransaction();
  94.         transaction.begin();
  95.  
  96.         final Client managedClient = entityManager.merge(client);
  97.  
  98.         entityManager.remove(managedClient);
  99.  
  100.         transaction.commit();
  101.         entityManager.close();
  102.     }
  103.  
  104.     @Override
  105.     public void deleteClient(int id) {
  106.         Client client = getClient(id);
  107.         deleteClient(client);
  108.         // Zaimplementować usuwanie klienta o podanym id
  109.         // Najpierw wydobyć klienta, potem go usunąć
  110.  
  111.         //TODO Implementacja
  112.     }
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top