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.ContragentsInShipment; import com.mycompany.CRMFly.entities.Documents; import com.mycompany.CRMFly.entities.Payments; import com.mycompany.CRMFly.entities.PositionsInShipment; import com.mycompany.CRMFly.entities.Products; 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 ShipmentsDAOImpl implements ShipmentsDAO { @Autowired private SessionFactory sessionFactory; @Autowired private DocumentsDAO documentsDAO; @Autowired private PaymentsDAO paymentsDAO; @Autowired private ProductsDAO productsDAO; @Override public void addShipment(Shipments shipment) { sessionFactory.getCurrentSession().save(shipment); List<Documents> temp = shipment.getDocumentsOnShipment(); List<Payments> temp2 = shipment.getPayments(); List<PositionsInShipment> temp3 = shipment.getProductPositions(); List<ContragentsInShipment> temp4 = shipment.getContragents(); if (temp2 != null && temp2.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("shipments-with-payments"); temp2 = paymentsDAO.getFromProxy(temp2); for (Payments payment : temp2) { payment.setShipment(shipment); paymentsDAO.changePayment(payment); } } if (temp != null && temp.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("shipments-with-documents"); temp = documentsDAO.getFromProxy(temp); for (Documents document : temp) { document.getShipmentsOnDocument().add(shipment); documentsDAO.changeDocument(document); } } if (temp4 != null && temp4.size() != 0) { for (ContragentsInShipment contragent : temp4) { contragent.setShipment(shipment); sessionFactory.getCurrentSession().save(contragent); } } if (temp3 != null && temp3.size() != 0) { for (PositionsInShipment position : temp3) { position.setShipment(shipment); sessionFactory.getCurrentSession().save(position); } } } @Override public List<Shipments> listShipments() { return sessionFactory.getCurrentSession().createQuery("from com.mycompany.CRMFly.entities.Shipments") .list(); } @Override public void removeShipment(Shipments shipment) { if (null != shipment) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); shipment = (Shipments) sess.get(Shipments.class, shipment.getId()); List<Documents> temp = shipment.getDocumentsOnShipment(); List<Payments> temp2 = shipment.getPayments(); List<PositionsInShipment> temp3 = shipment.getProductPositions(); List<ContragentsInShipment> temp4 = shipment.getContragents(); if (temp2 != null && temp2.size() != 0) { sess.enableFetchProfile("shipments-with-payments"); temp2 = paymentsDAO.getFromProxy(temp2); for (Payments payment : temp2) { payment.setShipment(null); sess.update(payment); } } if (temp != null && temp.size() != 0) { sess.enableFetchProfile("shipments-with-documents"); temp = documentsDAO.getFromProxy(temp); for (Documents document : temp) { document.getShipmentsOnDocument().remove(shipment); sess.update(document); } } if (temp4 != null && temp4.size() != 0) { for (ContragentsInShipment contragent : temp4) { sess.delete(contragent); } } if (temp3 != null && temp3.size() != 0) { for (PositionsInShipment position : temp3) { sess.delete(position); } } sess.update(shipment); sessionFactory.getCurrentSession().delete(shipment); } } @Override public void changeShipment(Shipments shipment) { sessionFactory.getCurrentSession().update(shipment); } @Override public Shipments getShipmentForId(Long id) { // return (Shipments) sessionFactory.getCurrentSession(). // load(Shipments.class, id); return (Shipments) sessionFactory.getCurrentSession().get(Shipments.class, id); } @Override public List<Shipments> getAllShipments() { return sessionFactory.getCurrentSession().createCriteria(Shipments.class).list(); } @Override public List<Payments> getPaymentsForShipment(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("shipments-with-payments"); Shipments shipment = (Shipments) sess.get(Shipments.class, id); return shipment.getPayments(); } @Override public List<Documents> getDocumentsForShipment(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("shipments-with-documents"); Shipments shipment = (Shipments) sess.get(Shipments.class, id); return shipment.getDocumentsOnShipment(); } @Override public List<ContragentsInShipment> getContragentsForShipment(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("shipments-with-contragents"); Shipments shipment = (Shipments) sess.get(Shipments.class, id); return shipment.getContragents(); } @Override public List<PositionsInShipment> getPositionsForShipment(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("shipments-with-productPos"); Shipments shipment = (Shipments) sess.get(Shipments.class, id); return shipment.getProductPositions(); } @Override public Clients getClientForPosition(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("shipments-with-clients"); ContragentsInShipment position = (ContragentsInShipment) sess.get(ContragentsInShipment.class, id); return position.getClient(); } @Override public Products getProductForPosition(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("shipments-with-products"); PositionsInShipment position = (PositionsInShipment) sess.get(PositionsInShipment.class, id); return position.getProduct(); } public PositionsInShipment getPositionForId(Long id) { return (PositionsInShipment) sessionFactory.getCurrentSession().get(PositionsInShipment.class, id); } public ContragentsInShipment getAgentForId(Long id) { return (ContragentsInShipment) sessionFactory.getCurrentSession().get(ContragentsInShipment.class, id); } public List<Shipments> getFromProxy(List<Shipments> proxy) { Disjunction or = Restrictions.disjunction(); for (Shipments pr : proxy) { or.add(Restrictions.eq("id", pr.getId())); } return sessionFactory.getCurrentSession().createCriteria(Shipments.class).add(or).list(); } }