Advertisement
andersonalmada

Untitled

Jul 8th, 2022
1,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package mandacaru3.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import jakarta.persistence.EntityManager;
  6. import jakarta.persistence.EntityManagerFactory;
  7. import jakarta.persistence.Persistence;
  8. import mandacaru3.entities.Product;
  9.  
  10. public class ProductHibernateDAO implements ProductDAO {
  11.  
  12.     static EntityManagerFactory sessionFactory = Persistence.createEntityManagerFactory("mandacaru");
  13.     static EntityManager entityManager;
  14.  
  15.     @Override
  16.     public void save(Product entity) {
  17.         entityManager = sessionFactory.createEntityManager();
  18.         entityManager.getTransaction().begin();
  19.  
  20.         if (entity.getId() == 0) {
  21.             entityManager.persist(entity);
  22.         } else {
  23.             entityManager.merge(entity);
  24.         }
  25.  
  26.         entityManager.getTransaction().commit();
  27.         entityManager.close();
  28.     }
  29.  
  30.     @Override
  31.     public void delete(int id) {
  32.         entityManager = sessionFactory.createEntityManager();
  33.         Product product = entityManager.find(Product.class, id);
  34.         entityManager.getTransaction().begin();
  35.         entityManager.remove(product);
  36.         entityManager.getTransaction().commit();
  37.         entityManager.close();
  38.     }
  39.  
  40.     @Override
  41.     public Product find(int id) {
  42.         entityManager = sessionFactory.createEntityManager();
  43.         Product product = entityManager.find(Product.class, id);
  44.         entityManager.close();
  45.  
  46.         return product;
  47.     }
  48.  
  49.     @Override
  50.     public List<Product> findAll() {
  51.         entityManager = sessionFactory.createEntityManager();
  52.         List<Product> products = entityManager.createQuery("from products", Product.class).getResultList();
  53.         entityManager.close();
  54.  
  55.         return products;
  56.     }
  57.  
  58.     @Override
  59.     public Product findByName(String str) {
  60.         entityManager = sessionFactory.createEntityManager();
  61.         Product product = null;
  62.  
  63.         try {
  64.             product = entityManager.createQuery("from products where name=?1", Product.class).setParameter(1, str)
  65.                     .getSingleResult();
  66.         } catch (Exception e) {
  67.             return null;
  68.         }
  69.         entityManager.close();
  70.  
  71.         return product;
  72.     }
  73.  
  74.     @Override
  75.     public List<Product> findAllByName(String str) {
  76.         entityManager = sessionFactory.createEntityManager();
  77.         List<Product> products = null;
  78.  
  79.         try {
  80.             products = entityManager.createQuery("from products where name=?1", Product.class)
  81.                     .setParameter(1, str).getResultList();
  82.         } catch (Exception e) {
  83.             return null;
  84.         }
  85.         entityManager.close();
  86.  
  87.         return products;
  88.     }
  89.  
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement