Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.mycompany.CRMFly.hibernateAccess; import com.mycompany.CRMFly.entities.Daily; import com.mycompany.CRMFly.entities.Events; import com.mycompany.CRMFly.entities.Projects; import java.util.List; import org.hibernate.SessionFactory; import org.hibernate.criterion.Disjunction; import org.hibernate.criterion.Restrictions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; /** * * @author ?? */ @Repository public class EventsDAOImpl implements EventsDAO { @Autowired private SessionFactory sessionFactory; @Autowired private ProjectsDAO projectsDAO; @Autowired private DailyDAO dailyDAO; @Override public void addEvent(Events event) { sessionFactory.getCurrentSession().save(event); List<Daily> temp = event.getConnectedTasks(); List<Projects> temp2 = event.getConnectedProjects(); if (temp != null && temp.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("events-with-tasks"); temp = dailyDAO.getFromProxy(temp); for (Daily task : temp) { task.getEventsConnectedWithTask().add(event); dailyDAO.changeTask(task); } } if (temp2 != null && temp2.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("events-with-projects"); temp2 = projectsDAO.getFromProxy(temp2); for (Projects project : temp2) { project.getEventsConnectedWithProjects().add(event); projectsDAO.changeProject(project); } } } @Override public List<Events> listEvents() { return sessionFactory.getCurrentSession().createQuery("from com.mycompany.CRMFly.entities.Events").list(); } @Override public void removeEvent(Events event) { if (null != event) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); event = (Events) sess.get(Events.class, event.getId()); List<Daily> temp = event.getConnectedTasks(); List<Projects> temp2 = event.getConnectedProjects(); if (temp != null && temp.size() != 0) { sess.enableFetchProfile("events-with-tasks"); temp = dailyDAO.getFromProxy(temp); for (Daily task : temp) { task.getEventsConnectedWithTask().remove(event); sess.update(task); } } if (temp2 != null && temp2.size() != 0) { sess.enableFetchProfile("events-with-projects"); temp2 = projectsDAO.getFromProxy(temp2); for (Projects project : temp2) { project.getEventsConnectedWithProjects().remove(event); sess.update(project); } } sess.update(event); sessionFactory.getCurrentSession().delete(event); } } @Override public void changeEvent(Events event) { sessionFactory.getCurrentSession().update(event); } @Override public Events getEventForId(Long id) { // return (Events) sessionFactory.getCurrentSession(). // load(Events.class, id); return (Events) sessionFactory.getCurrentSession().get(Events.class, id); } public Events getEventForName(String name) { return (Events) sessionFactory.getCurrentSession().createCriteria(Events.class) .add(Restrictions.eq("name", name)).uniqueResult(); } @Override public List<Events> getAllEvents() { return sessionFactory.getCurrentSession().createCriteria(Events.class).list(); } @Override public List<Daily> getTasksOnEvent(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("events-with-tasks"); Events event = (Events) sess.get(Events.class, id); return event.getConnectedTasks(); } @Override public List<Projects> getProjectsOnEvent(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("events-with-projects"); Events event = (Events) sess.get(Events.class, id); return event.getConnectedProjects(); } public List<Events> getFromProxy(List<Events> proxy) { Disjunction or = Restrictions.disjunction(); for (Events pr : proxy) { or.add(Restrictions.eq("id", pr.getId())); } return sessionFactory.getCurrentSession().createCriteria(Events.class).add(or).list(); } public List<Events> getEventsForEmployee(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("events-with-tasks"); sess.enableFetchProfile("events-with-projects"); List<Events> first = sess.createCriteria(Events.class).createCriteria("connectedTasks") .createCriteria("implementer").add(Restrictions.eq("id", id)).list(); List<Events> second = sess.createCriteria(Events.class).createCriteria("connectedProjects") .createCriteria("participants").add(Restrictions.eq("id", id)).list(); first.addAll(second); return first; } }