Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package ejbs;
  2.  
  3. import entities.Document;
  4. import entities.Student;
  5. import exceptions.MyEntityNotFoundException;
  6.  
  7. import javax.ejb.EJBException;
  8. import javax.ejb.Stateless;
  9. import javax.persistence.EntityManager;
  10. import javax.persistence.PersistenceContext;
  11. import java.util.List;
  12.  
  13. @Stateless(name = "DocumentEJB")
  14. public class DocumentBean {
  15. @PersistenceContext
  16. EntityManager em;
  17.  
  18. public void create(
  19. String username,
  20. String filepath,
  21. String fileName)
  22. throws MyEntityNotFoundException{
  23. try {
  24. Student student = em.find(Student.class, username);
  25. if(student == null){
  26. throw new MyEntityNotFoundException("Student with username: " + username + " not found");
  27. }
  28. Document document = new Document(filepath, fileName, student);
  29. em.persist(document);
  30. student.addDocument(document);
  31. } catch (MyEntityNotFoundException e) {
  32. throw e;
  33. } catch (Exception e) {
  34. throw new EJBException("ERROR_CREATING_DOCUMENT ----> ", e);
  35. }
  36. }
  37.  
  38. public Document findDocument(int id) {
  39. try{
  40. return em.find(Document.class, id);
  41. } catch (Exception e) {
  42. throw new EJBException("ERROR_FINDING_DOCUMENT ----> ", e);
  43. }
  44. }
  45.  
  46. public List<Document> getStudentDocuments(String username) {
  47. try{
  48. return em.createNamedQuery("getStudentDocuments", Document.class).setParameter("username", username).getResultList();
  49. } catch (Exception e) {
  50. throw new EJBException("ERROR_FINDING_DOCUMENT ----> ", e);
  51. }
  52. }
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement