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.Clients; import com.mycompany.CRMFly.entities.Contracts; import com.mycompany.CRMFly.entities.Documents; import com.mycompany.CRMFly.entities.Payments; import com.mycompany.CRMFly.entities.Shipments; 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 DocumentsDAOImpl implements DocumentsDAO { @Autowired private SessionFactory sessionFactory; @Autowired private ContractsDAO contractsDAO; @Autowired private PaymentsDAO paymentsDAO; @Autowired private ShipmentsDAO shipmentsDAO; @Autowired private ClientsDAO clientsDAO; @Override public void addDocument(Documents document) { sessionFactory.getCurrentSession().save(document); List<Contracts> temp; List<Payments> temp2 = document.getPaymentsOnDocument(); List<Shipments> temp3 = document.getShipmentsOnDocument(); List<Clients> temp4 = document.getClients(); if (temp4 != null && temp4.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("documents-with-clients"); temp4 = clientsDAO.getFromProxy(temp4); for (Clients client : temp4) { client.getDocuments().add(document); clientsDAO.changeClient(client); } } if (temp2 != null && temp2.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("documents-with-payments"); temp2 = paymentsDAO.getFromProxy(temp2); for (Payments payment : temp2) { payment.setDocumentOnPayment(document); paymentsDAO.changePayment(payment); } } if (temp3 != null && temp3.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("documents-with-shipments"); temp3 = shipmentsDAO.getFromProxy(temp3); for (Shipments shipment : temp3) { shipment.getDocumentsOnShipment().add(document); shipmentsDAO.changeShipment(shipment); } } } @Override public List<Documents> listDocuments() { return sessionFactory.getCurrentSession().createQuery("from com.mycompany.CRMFly.entities.Documents") .list(); } @Override public void removeDocument(Documents document) { if (null != document) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); document = (Documents) sess.get(Documents.class, document.getId()); List<Contracts> temp; List<Payments> temp2 = document.getPaymentsOnDocument(); List<Shipments> temp3 = document.getShipmentsOnDocument(); List<Clients> temp4 = document.getClients(); if (temp4 != null && temp4.size() != 0) { sess.enableFetchProfile("documents-with-clients"); temp4 = clientsDAO.getFromProxy(temp4); for (Clients client : temp4) { client.getDocuments().remove(document); sess.update(client); } } if (temp2 != null && temp2.size() != 0) { sess.enableFetchProfile("documents-with-payments"); temp2 = paymentsDAO.getFromProxy(temp2); for (Payments payment : temp2) { payment.setDocumentOnPayment(null); sess.update(payment); } } if (temp3 != null && temp3.size() != 0) { sess.enableFetchProfile("documents-with-shipments"); temp3 = shipmentsDAO.getFromProxy(temp3); for (Shipments shipment : temp3) { shipment.getDocumentsOnShipment().remove(document); sess.update(shipment); } } sess.update(document); sessionFactory.getCurrentSession().delete(document); } } @Override public void changeDocument(Documents document) { sessionFactory.getCurrentSession().update(document); } @Override public Documents getDocumentForId(Long id) { // return (Documents) sessionFactory.getCurrentSession(). // load(Documents.class, id); return (Documents) sessionFactory.getCurrentSession().get(Documents.class, id); } @Override public List<Documents> getAllDocuments() { return sessionFactory.getCurrentSession().createCriteria(Documents.class).list(); } @Override public Contracts getContractOnDocument(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("documents-with-contracts"); Documents document = (Documents) sess.get(Documents.class, id); return document.getContract(); } @Override public List<Clients> getSignersOfDocument(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("documents-with-clients"); Documents document = (Documents) sess.get(Documents.class, id); return document.getClients(); } @Override public List<Payments> getPaymentsOnDocument(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("documents-with-payments"); Documents document = (Documents) sess.get(Documents.class, id); return document.getPaymentsOnDocument(); } @Override public List<Shipments> getShipmentsOnDocument(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("documents-with-shipments"); Documents document = (Documents) sess.get(Documents.class, id); return document.getShipmentsOnDocument(); } public List<Documents> getFromProxy(List<Documents> proxy) { Disjunction or = Restrictions.disjunction(); for (Documents pr : proxy) { or.add(Restrictions.eq("id", pr.getId())); } return sessionFactory.getCurrentSession().createCriteria(Documents.class).add(or).list(); } }