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.Contacts; import com.mycompany.CRMFly.entities.ContragentsInContract; import com.mycompany.CRMFly.entities.Documents; import com.mycompany.CRMFly.entities.Payments; import com.mycompany.CRMFly.entities.PositionsInContract; import com.mycompany.CRMFly.entities.Products; import com.mycompany.CRMFly.entities.Requests; 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 RequestsDAOImpl implements RequestsDAO { @Autowired private SessionFactory sessionFactory; @Autowired private ContactsDAO contactsDAO; @Autowired private ProductsDAO productsDAO; @Override public void addRequest(Requests request) { sessionFactory.getCurrentSession().save(request); List<Contacts> contTemp = request.getContacts(); List<Products> prodTemp = request.getProducts(); if (contTemp != null && contTemp.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("requests-with-products"); contTemp = contactsDAO.getFromProxy(contTemp); for (Contacts contact : contTemp) { contact.getConnectedRequests().add(request); contactsDAO.changeContact(contact); } } if (prodTemp != null && prodTemp.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("requests-with-products"); prodTemp = productsDAO.getFromProxy(prodTemp); for (Products product : prodTemp) { product.getRequestsForProduct().add(request); productsDAO.changeProduct(product); } } } @Override public List<Requests> listRequests() { return sessionFactory.getCurrentSession().createQuery("from com.mycompany.CRMFly.entities.Requests").list(); } @Override public void removeRequest(Requests request) { if (null != request) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); request = (Requests) sess.get(Requests.class, request.getId()); List<Contacts> contTemp = request.getContacts(); List<Products> prodTemp = request.getProducts(); if (contTemp != null && contTemp.size() != 0) { sess.enableFetchProfile("requests-with-products"); contTemp = contactsDAO.getFromProxy(contTemp); for (Contacts contact : contTemp) { contact.getConnectedRequests().remove(request); sess.update(contact); } } if (prodTemp != null && prodTemp.size() != 0) { sess.enableFetchProfile("requests-with-products"); prodTemp = productsDAO.getFromProxy(prodTemp); for (Products product : prodTemp) { product.getRequestsForProduct().remove(request); sess.update(product); } } sess.update(request); sessionFactory.getCurrentSession().delete(request); } } @Override public void changeRequest(Requests request) { sessionFactory.getCurrentSession().update(request); } @Override public Requests getRequestForId(Long id) { // return (Contracts) sessionFactory.getCurrentSession(). // load(Contracts.class, id); return (Requests) sessionFactory.getCurrentSession().get(Requests.class, id); } @Override public List<Requests> getAllRequests() { return sessionFactory.getCurrentSession().createCriteria(Requests.class).list(); } @Override public List<Contacts> getAllContacts(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("requests-with-contacts"); Requests request = (Requests) sess.get(Requests.class, id); return request.getContacts(); } @Override public List<Products> getAllProducts(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("requests-with-products"); Requests request = (Requests) sess.get(Requests.class, id); return request.getProducts(); } @Override public List<Requests> getFromProxy(List<Requests> proxy) { Disjunction or = Restrictions.disjunction(); for (Requests pr : proxy) { or.add(Restrictions.eq("id", pr.getId())); } return sessionFactory.getCurrentSession().createCriteria(Requests.class).add(or).list(); } }