Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. package edu.pjwstk.sri.lab2.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.ejb.Stateless;
  6. import javax.ejb.*;
  7. import javax.persistence.EntityManager;
  8. import javax.persistence.PersistenceContext;
  9. import javax.persistence.TypedQuery;
  10.  
  11. import edu.pjwstk.sri.lab2.model.Category;
  12.  
  13. /**
  14. * DAO for Category
  15. */
  16. @Stateless
  17. @TransactionManagement(TransactionManagementType.CONTAINER)
  18. public class CategoryDao {
  19. @PersistenceContext(unitName = "sri2-persistence-unit")
  20. private EntityManager em;
  21.  
  22. // wymagana transakcja podczas dodawania
  23. @TransactionAttribute(TransactionAttributeType.REQUIRED)
  24. public void create(Category entity) {
  25. em.persist(entity);
  26. }
  27.  
  28. // wymagana transakcja podczas usuwania
  29. @TransactionAttribute(TransactionAttributeType.REQUIRED)
  30. public void deleteById(Long id) {
  31. Category entity = em.find(Category.class, id);
  32. if (entity != null) {
  33. em.remove(entity);
  34. }
  35. }
  36.  
  37. // odczytywanie wartosci wiec nie wymaga transakcji
  38. @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
  39. public Category findById(Long id) {
  40. return em.find(Category.class, id);
  41. }
  42.  
  43. // wymagana transakcja podczas update
  44. @TransactionAttribute(TransactionAttributeType.REQUIRED)
  45. public Category update(Category entity) {
  46. return em.merge(entity);
  47. }
  48.  
  49. // odczytywanie wartosci wiec nie wymaga transakcji
  50. @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
  51. public List<Category> listAll(Integer startPosition, Integer maxResult) {
  52. TypedQuery<Category> findAllQuery = em
  53. .createQuery(
  54. "SELECT DISTINCT c FROM Category c LEFT JOIN FETCH c.parentCategory LEFT JOIN FETCH c.childCategories ORDER BY c.id",
  55. Category.class);
  56. if (startPosition != null) {
  57. findAllQuery.setFirstResult(startPosition);
  58. }
  59. if (maxResult != null) {
  60. findAllQuery.setMaxResults(maxResult);
  61. }
  62. return findAllQuery.getResultList();
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement