SHARE
TWEET

Untitled

a guest Dec 8th, 2019 119 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. }
  114.  
  115.  
  116.  
  117.  
  118. package pl.polsl.zti.db1.domain;
  119.  
  120. import java.io.Serializable;
  121. import java.util.Date;
  122.  
  123. import javax.persistence.Column;
  124. import javax.persistence.Entity;
  125. import javax.persistence.FetchType;
  126. import javax.persistence.GeneratedValue;
  127. import javax.persistence.Id;
  128. import javax.persistence.JoinColumn;
  129. import javax.persistence.ManyToOne;
  130. import javax.persistence.Table;
  131.  
  132. @Entity
  133. @Table(name = "ORDERS")
  134. public class Order implements Serializable {
  135.  
  136.     private static final long serialVersionUID = 1L;
  137.     @Id @GeneratedValue
  138.     @Column(name = "id")
  139.     private int id;
  140.     @Column(name = "order_no")
  141.     private String no;
  142.     @ManyToOne(fetch = FetchType.LAZY)
  143.     @JoinColumn(name = "CLIENT_ID", nullable = false)
  144.     private Client client;
  145.     @Column(name = "ORDER_DATE")
  146.     private Date date;
  147.     @Column(name = "ORDER_DESC")
  148.     private String description;
  149.  
  150.     public int getId() {
  151.         return id;
  152.     }
  153.  
  154.     public void setId(int id) {
  155.         this.id = id;
  156.     }
  157.  
  158.     public String getNo() {
  159.         return no;
  160.     }
  161.  
  162.     public void setNo(String no) {
  163.         this.no = no;
  164.     }
  165.  
  166.     public Client getClient() {
  167.         return client;
  168.     }
  169.  
  170.     public void setClient(Client client) {
  171.         this.client = client;
  172.     }
  173.  
  174.     public Date getDate() {
  175.         return date;
  176.     }
  177.  
  178.     public void setDate(Date date) {
  179.         this.date = date;
  180.     }
  181.  
  182.     public String getDescription() {
  183.         return description;
  184.     }
  185.  
  186.     public void setDescription(String description) {
  187.         this.description = description;
  188.     }
  189. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top