tdmcginley

Services.java

May 13th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1.  
  2. package services;
  3.  
  4. import org.springframework.transaction.annotation.Transactional;
  5. import org.hibernate.Query;
  6. import org.hibernate.SessionFactory;
  7. import org.hibernate.Session;
  8.  
  9. import data.*;
  10. import java.util.List;
  11.  
  12. // This class is the business services tier in the application.
  13. // @Transactional is needed so that a Hibernate transaction is set up,
  14. // otherwise updates won't have an affect
  15. @Transactional
  16. public class Services {
  17. // So Spring can inject the session factory
  18. SessionFactory sessionFactory;
  19. public void setSessionFactory(SessionFactory value) {
  20. sessionFactory = value;
  21. }
  22.  
  23. // Shortcut for sessionFactory.getCurrentSession()
  24. public Session sess() {
  25. return sessionFactory.getCurrentSession();
  26. }
  27.  
  28. public Forms getFormsById(Long id) {
  29. return (Forms) sess().load(Forms.class, id);
  30. }
  31.  
  32. @SuppressWarnings("unchecked")
  33. public List<Forms> getForms() {
  34. return sess().createQuery("from Forms").list();
  35. }
  36.  
  37. @SuppressWarnings("unchecked")
  38. public List<Questions> getQuestions() {
  39. return sess().createQuery("from Questions").list();
  40. }
  41.  
  42. public Questions getQuestionById(Long id) {
  43. return (Questions) sess().load(Questions.class, id);
  44. }
  45.  
  46. @SuppressWarnings("unchecked")
  47. public List<Questions> getQuestionsByForm(Long formId) {
  48. // return (Questions) sess().load(Questions.class, formId);
  49. Query queryQuestion = sess().createQuery("from Questions where form_id = :formId");
  50. queryQuestion.setParameter("formId", formId);
  51. return queryQuestion.list();
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment