Guest User

Untitled

a guest
Nov 26th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. package ro.unitbv.mi.javaweb.service.impl;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Properties;
  8. import java.util.concurrent.atomic.AtomicLong;
  9.  
  10. import org.hibernate.HibernateException;
  11. import org.hibernate.SessionFactory;
  12. import org.hibernate.HibernateException;
  13. import org.hibernate.Session;
  14. import org.hibernate.SessionFactory;
  15. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  16. import org.hibernate.cfg.Configuration;
  17. import org.hibernate.service.ServiceRegistry;
  18. import org.springframework.stereotype.Service;
  19.  
  20. import ro.unitbv.mi.javaweb.model.Person;
  21. import ro.unitbv.mi.javaweb.service.PersonService;
  22.  
  23. import org.hibernate.HibernateException;
  24. import org.hibernate.Query;
  25. import org.hibernate.Session;
  26. import org.hibernate.SessionFactory;
  27.  
  28. @Service
  29. public class PersonServiceImpl implements PersonService {
  30.  
  31. private List<Person> persons = new ArrayList<Person>();
  32. private AtomicLong idGenerator = new AtomicLong(0);
  33.  
  34. private SessionFactory sessionFactory = null;
  35.  
  36. public SessionFactory configureSessionFactory() throws HibernateException {
  37. if (null != sessionFactory)
  38. return sessionFactory;
  39.  
  40. Configuration configuration = new Configuration();
  41. configuration.addResource("hibernate.cfg.xml").configure();
  42.  
  43. Properties properties = configuration.getProperties();
  44.  
  45. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(properties).build();
  46. sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  47.  
  48. return sessionFactory;
  49. }
  50.  
  51. public List<Person> listPersons() {
  52. synchronized (persons) {
  53. sessionFactory = configureSessionFactory();
  54.  
  55. Session sess = null;
  56.  
  57. try {
  58. sess = sessionFactory.openSession();
  59. sess.beginTransaction();
  60.  
  61. persons = sess.createQuery("from Person").list();
  62.  
  63. sess.flush();
  64. sess.getTransaction().commit();
  65.  
  66. } catch (HibernateException he) {
  67. he.printStackTrace();
  68.  
  69. sess.getTransaction().rollback();
  70. } finally {
  71. sess.close();
  72. sessionFactory.close();
  73. }
  74.  
  75. return persons;
  76.  
  77. }
  78. }
  79.  
  80. public long addPerson(Person person) {
  81. synchronized (persons) {
  82. sessionFactory = configureSessionFactory();
  83.  
  84. Session sess = null;
  85.  
  86. try {
  87. sess = sessionFactory.openSession();
  88. sess.beginTransaction();
  89.  
  90. sess.persist(person);
  91.  
  92. sess.flush();
  93. sess.getTransaction().commit();
  94.  
  95. } catch (HibernateException he) {
  96. he.printStackTrace();
  97.  
  98. sess.getTransaction().rollback();
  99. } finally {
  100. sess.close();
  101. sessionFactory.close();
  102. }
  103.  
  104. return person.getId();
  105.  
  106. // Person clonedPerson = new Person(person);
  107. //
  108. // clonedPerson.setId(idGenerator.incrementAndGet());
  109. // persons.add(clonedPerson);
  110. // return clonedPerson.getId();
  111. }
  112. }
  113.  
  114. public boolean removePerson(long id) {
  115. synchronized (persons) {
  116. sessionFactory = configureSessionFactory();
  117.  
  118. Session sess = null;
  119.  
  120. try {
  121. sess = sessionFactory.openSession();
  122. sess.beginTransaction();
  123.  
  124. sess.delete(id);
  125.  
  126. sess.flush();
  127. sess.getTransaction().commit();
  128.  
  129. } catch (HibernateException he) {
  130. he.printStackTrace();
  131.  
  132. sess.getTransaction().rollback();
  133. } finally {
  134. sess.close();
  135. sessionFactory.close();
  136. }
  137.  
  138. return true;
  139.  
  140. // return persons.removeIf(p -> p.getId() == id);
  141. //
  142. // Iterator<Person> iterator = persons.iterator();
  143. // Person p;
  144. // while (iterator.hasNext()) {
  145. // p = iterator.next();
  146. // if (p.getId() == id) {
  147. // iterator.remove();
  148. // return true;
  149. // }
  150. // }
  151. }
  152. //return false;
  153. }
  154.  
  155. public void close() throws IOException {
  156. synchronized (persons) {
  157. persons.clear();
  158. }
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment