Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.andyshon;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import javax.swing.*;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- /**
- * Created by andys_000 on 03.10.2017.
- */
- public class DriverDAOImpl implements DriverDAO{
- public void addDriver(Driver driver) throws SQLException {
- Session session = null;
- try {
- session = HibernateUtil.getSessionFactory().openSession();
- session.beginTransaction();
- session.save(driver);
- session.getTransaction().commit();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка при вставке", JOptionPane.OK_OPTION);
- } finally {
- if (session != null && session.isOpen()) {
- session.close();
- }
- }
- }
- public void updateDriver(int driver_id, Driver driver) throws SQLException {
- Session session = null;
- try {
- session = HibernateUtil.getSessionFactory().openSession();
- session.beginTransaction();
- session.update(driver);
- session.getTransaction().commit();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка при вставке", JOptionPane.OK_OPTION);
- } finally {
- if (session != null && session.isOpen()) {
- session.close();
- }
- }
- }
- public Driver getDriverById(int driver_id) throws SQLException {
- Session session = null;
- Driver driver = null;
- try {
- session = HibernateUtil.getSessionFactory().openSession();
- driver = (Driver) session.get(Driver.class, driver_id); // get, not load!!
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка 'findById'", JOptionPane.OK_OPTION);
- } finally {
- if (session != null && session.isOpen()) {
- session.close();
- }
- }
- return driver;
- }
- public Collection getAllDrivers() throws SQLException {
- Session session = null;
- List drivers = new ArrayList<Driver>();
- try {
- session = HibernateUtil.getSessionFactory().openSession();
- drivers = session.createCriteria(Driver.class).list();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка 'getAll'", JOptionPane.OK_OPTION);
- } finally {
- if (session != null && session.isOpen()) {
- session.close();
- }
- }
- return drivers;
- }
- public void deleteDriver(Driver driver) throws SQLException {
- Session session = null;
- try {
- session = HibernateUtil.getSessionFactory().openSession();
- session.beginTransaction();
- session.delete(driver);
- session.getTransaction().commit();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, e.getMessage(), "Ошибка при удалении", JOptionPane.OK_OPTION);
- } finally {
- if (session != null && session.isOpen()) {
- session.close();
- }
- }
- }
- public Collection getDriversByBus(Bus bus) throws SQLException {
- Session session = null;
- List drivers = new ArrayList<Driver>();
- try {
- session = HibernateUtil.getSessionFactory().getCurrentSession();
- session.beginTransaction();
- int bus_id = bus.getId();
- Query query = session.createQuery(
- " select b "
- + " from Driver b INNER JOIN b.busses bus"
- + " where bus.id = :busId "
- )
- .setLong("busId", bus_id);
- drivers = (List<Driver>) query.list();
- session.getTransaction().commit();
- } finally {
- if (session != null && session.isOpen()) {
- session.close();
- }
- }
- return drivers;
- }
- }
Add Comment
Please, Sign In to add comment