Advertisement
Guest User

Untitled

a guest
May 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. package dao;
  2.  
  3. import models.Account;
  4. import models.Unit;
  5. import org.hibernate.Session;
  6. import org.hibernate.Transaction;
  7. import utils.HibernateSessionFactoryUtil;
  8.  
  9. import java.sql.PreparedStatement;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. public class InvUnitDAO<T> implements InvDAO<T> {
  14.  
  15.  
  16. @Override
  17. public void add(T data) {
  18. Session session = HibernateSessionFactoryUtil.getSessionFactory().openSession();
  19. Transaction unitTransaction = session.beginTransaction();
  20. session.save(data);
  21. unitTransaction.commit();
  22. session.close();
  23. }
  24.  
  25.  
  26. @Override
  27. public void del(T data) {
  28. Session session = HibernateSessionFactoryUtil
  29. .getSessionFactory()
  30. .openSession();
  31. Transaction delTransaction = session.beginTransaction();
  32. session.delete(data);
  33. delTransaction.commit();
  34. session.close();
  35. }
  36.  
  37. @Override
  38. public List<Account> getAllAccounts() {
  39. Session session = HibernateSessionFactoryUtil
  40. .getSessionFactory()
  41. .openSession();
  42. List<Account> accounts = (List<Account>) session.createQuery("from Account").getResultList();
  43. session.close();
  44. return accounts;
  45. }
  46.  
  47. @Override
  48. public ArrayList<Unit> getAllUnits() {
  49. Session session = HibernateSessionFactoryUtil
  50. .getSessionFactory()
  51. .openSession();
  52. ArrayList<Unit> units = (ArrayList<Unit>) session.createQuery("From Unit ").getResultList();
  53. session.close();
  54. return units;
  55. }
  56.  
  57. public ArrayList<String> getPasswords(){
  58. Session session = HibernateSessionFactoryUtil
  59. .getSessionFactory()
  60. .openSession();
  61. ArrayList<String> passwords = (ArrayList<String>) session.createQuery("SELECT password FROM Account ").getResultList();
  62. session.close();
  63. return passwords;
  64. }
  65.  
  66. // public ArrayList<T> found(String str){
  67. // String qwerty = "from inventorydb where invNum = :invNum";
  68. // Session session = HibernateSessionFactoryUtil
  69. // .getSessionFactory()
  70. // .openSession();
  71. // Transaction transaction = session.beginTransaction();
  72. // PreparedStatement statement = {
  73. // }
  74. // ArrayList<T> results = (ArrayList<T>) session.createQuery(" FROM Account WHERE ")
  75. // }
  76.  
  77. public void update(T data){
  78. Session session = HibernateSessionFactoryUtil
  79. .getSessionFactory()
  80. .openSession();
  81. Transaction transaction = session.beginTransaction();
  82. session.update(data);
  83. transaction.commit();
  84. session.close();
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement