Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ro.unitbv.mi.javaweb.service.impl;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Properties;
- import java.util.concurrent.atomic.AtomicLong;
- import org.hibernate.HibernateException;
- import org.hibernate.SessionFactory;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import org.springframework.stereotype.Service;
- import ro.unitbv.mi.javaweb.model.Person;
- import ro.unitbv.mi.javaweb.service.PersonService;
- import org.hibernate.HibernateException;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- @Service
- public class PersonServiceImpl implements PersonService {
- private List<Person> persons = new ArrayList<Person>();
- private AtomicLong idGenerator = new AtomicLong(0);
- private SessionFactory sessionFactory = null;
- public SessionFactory configureSessionFactory() throws HibernateException {
- if (null != sessionFactory)
- return sessionFactory;
- Configuration configuration = new Configuration();
- configuration.addResource("hibernate.cfg.xml").configure();
- Properties properties = configuration.getProperties();
- ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(properties).build();
- sessionFactory = configuration.buildSessionFactory(serviceRegistry);
- return sessionFactory;
- }
- public List<Person> listPersons() {
- synchronized (persons) {
- sessionFactory = configureSessionFactory();
- Session sess = null;
- try {
- sess = sessionFactory.openSession();
- sess.beginTransaction();
- persons = sess.createQuery("from Person").list();
- sess.flush();
- sess.getTransaction().commit();
- } catch (HibernateException he) {
- he.printStackTrace();
- sess.getTransaction().rollback();
- } finally {
- sess.close();
- sessionFactory.close();
- }
- return persons;
- }
- }
- public long addPerson(Person person) {
- synchronized (persons) {
- sessionFactory = configureSessionFactory();
- Session sess = null;
- try {
- sess = sessionFactory.openSession();
- sess.beginTransaction();
- sess.persist(person);
- sess.flush();
- sess.getTransaction().commit();
- } catch (HibernateException he) {
- he.printStackTrace();
- sess.getTransaction().rollback();
- } finally {
- sess.close();
- sessionFactory.close();
- }
- return person.getId();
- // Person clonedPerson = new Person(person);
- //
- // clonedPerson.setId(idGenerator.incrementAndGet());
- // persons.add(clonedPerson);
- // return clonedPerson.getId();
- }
- }
- public boolean removePerson(long id) {
- synchronized (persons) {
- sessionFactory = configureSessionFactory();
- Session sess = null;
- try {
- sess = sessionFactory.openSession();
- sess.beginTransaction();
- sess.delete(id);
- sess.flush();
- sess.getTransaction().commit();
- } catch (HibernateException he) {
- he.printStackTrace();
- sess.getTransaction().rollback();
- } finally {
- sess.close();
- sessionFactory.close();
- }
- return true;
- // return persons.removeIf(p -> p.getId() == id);
- //
- // Iterator<Person> iterator = persons.iterator();
- // Person p;
- // while (iterator.hasNext()) {
- // p = iterator.next();
- // if (p.getId() == id) {
- // iterator.remove();
- // return true;
- // }
- // }
- }
- //return false;
- }
- public void close() throws IOException {
- synchronized (persons) {
- persons.clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment