andyshon

DriverDAOImpl.java

Oct 4th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. package com.andyshon;
  2.  
  3. import org.hibernate.Query;
  4. import org.hibernate.Session;
  5.  
  6. import javax.swing.*;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.Collection;
  10. import java.util.List;
  11.  
  12. /**
  13.  * Created by andys_000 on 03.10.2017.
  14.  */
  15. public class DriverDAOImpl implements DriverDAO{
  16.     public void addDriver(Driver driver) throws SQLException {
  17.         Session session = null;
  18.         try {
  19.             session = HibernateUtil.getSessionFactory().openSession();
  20.             session.beginTransaction();
  21.             session.save(driver);
  22.             session.getTransaction().commit();
  23.         } catch (Exception e) {
  24.             JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка при вставке", JOptionPane.OK_OPTION);
  25.         } finally {
  26.             if (session != null && session.isOpen()) {
  27.  
  28.                 session.close();
  29.             }
  30.         }
  31.     }
  32.  
  33.     public void updateDriver(int driver_id, Driver driver) throws SQLException {
  34.         Session session = null;
  35.         try {
  36.             session = HibernateUtil.getSessionFactory().openSession();
  37.             session.beginTransaction();
  38.             session.update(driver);
  39.             session.getTransaction().commit();
  40.         } catch (Exception e) {
  41.             JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка при вставке", JOptionPane.OK_OPTION);
  42.         } finally {
  43.             if (session != null && session.isOpen()) {
  44.                 session.close();
  45.             }
  46.         }
  47.     }
  48.  
  49.     public Driver getDriverById(int driver_id) throws SQLException {
  50.         Session session = null;
  51.         Driver driver = null;
  52.         try {
  53.             session = HibernateUtil.getSessionFactory().openSession();
  54.             driver = (Driver) session.get(Driver.class, driver_id); // get, not load!!
  55.         } catch (Exception e) {
  56.             JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка 'findById'", JOptionPane.OK_OPTION);
  57.         } finally {
  58.             if (session != null && session.isOpen()) {
  59.                 session.close();
  60.             }
  61.         }
  62.         return driver;
  63.     }
  64.  
  65.     public Collection getAllDrivers() throws SQLException {
  66.         Session session = null;
  67.         List drivers = new ArrayList<Driver>();
  68.         try {
  69.             session = HibernateUtil.getSessionFactory().openSession();
  70.             drivers = session.createCriteria(Driver.class).list();
  71.         } catch (Exception e) {
  72.             JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка 'getAll'", JOptionPane.OK_OPTION);
  73.         } finally {
  74.             if (session != null && session.isOpen()) {
  75.                 session.close();
  76.             }
  77.         }
  78.         return drivers;
  79.     }
  80.  
  81.     public void deleteDriver(Driver driver) throws SQLException {
  82.         Session session = null;
  83.         try {
  84.             session = HibernateUtil.getSessionFactory().openSession();
  85.             session.beginTransaction();
  86.             session.delete(driver);
  87.             session.getTransaction().commit();
  88.         } catch (Exception e) {
  89.             JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка при удалении", JOptionPane.OK_OPTION);
  90.         } finally {
  91.             if (session != null && session.isOpen()) {
  92.                 session.close();
  93.             }
  94.         }
  95.     }
  96.  
  97.     public Collection getDriversByBus(Bus bus) throws SQLException {
  98.         Session session = null;
  99.         List drivers = new ArrayList<Driver>();
  100.         try {
  101.             session = HibernateUtil.getSessionFactory().getCurrentSession();
  102.             session.beginTransaction();
  103.             int bus_id = bus.getId();
  104.             Query query = session.createQuery(
  105.                     " select b "
  106.                             + " from Driver b INNER JOIN b.busses bus"
  107.                             + " where bus.id = :busId "
  108.             )
  109.                     .setLong("busId", bus_id);
  110.             drivers = (List<Driver>) query.list();
  111.             session.getTransaction().commit();
  112.  
  113.         } finally {
  114.             if (session != null && session.isOpen()) {
  115.                 session.close();
  116.             }
  117.         }
  118.         return drivers;
  119.     }
  120. }
Add Comment
Please, Sign In to add comment