Example usage for org.hibernate Query setParameter

List of usage examples for org.hibernate Query setParameter

Introduction

In this page you can find the example usage for org.hibernate Query setParameter.

Prototype

@SuppressWarnings("unchecked")
Query<R> setParameter(int position, Object val);

Source Link

Document

Bind a positional query parameter using its inferred Type.

Usage

From source file:NewJFrame.java

private void removeMovieButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeMovieButtonActionPerformed
    // TODO add your handling code here:

    int id = Integer
            .parseInt(removeMovieTable.getModel().getValueAt(removeMovieTable.getSelectedRow(), 0).toString());
    try {//from  www .j av  a 2s  . com
        session = NewHibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
        Query query = session.createQuery("delete GatunekFilm where id.idFilmu =:id");
        query.setParameter("id", id);
        int result1 = query.executeUpdate();
        session.getTransaction().commit();
        session.beginTransaction();
        query = session.createQuery("delete Film where idFilmu =:id");
        query.setParameter("id", id);
        int result = query.executeUpdate();

        if (result > 0) {
            removeMovieSuccess.setVisible(true);
        }

    } catch (HibernateException he) {
        he.printStackTrace();
        removeMovieError.setVisible(true);
    } finally {
        session.getTransaction().commit();
        session.close();
        reloadRemoveMoviesTable();

    }

}

From source file:NewJFrame.java

private void orderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orderButtonActionPerformed
    if (moviesTable.getSelectedRow() != -1) {
        if (moviesTable.getValueAt(moviesTable.getSelectedRow(), 4).toString().equalsIgnoreCase("Dostpny")) {
            session = NewHibernateUtil.getSessionFactory().openSession();
            try {

                Transakcja t;/*from  ww  w  .  j av  a 2s. c  om*/
                t = new Transakcja();
                t.setIdKlienta(loggedId);
                t.setIdFilmu(Integer.parseInt(
                        moviesTable.getModel().getValueAt(moviesTable.getSelectedRow(), 0).toString()));
                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                Date date = new Date();
                t.setDataTransakcji(date);
                t.setTyp("WYP");
                session.beginTransaction();
                session.save(t);
                session.getTransaction().commit();
                session.beginTransaction();
                Query query = session.createQuery("update Film set ilosc =ilosc - 1 where idFilmu =:id");
                query.setParameter("id", Integer.parseInt(
                        moviesTable.getModel().getValueAt(moviesTable.getSelectedRow(), 0).toString()));
                query.executeUpdate();
                session.getTransaction().commit();
                session.flush();
                moviesPanel.setVisible(false);
                showMovies();
                moviesPanel.setVisible(true);

            } catch (HibernateException he) {
                he.printStackTrace();
            }
            session.close();
        }
    }
}

From source file:NewJFrame.java

private void acceptTransactionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptTransactionButtonActionPerformed
    if (transactionTable.getSelectedRow() != -1) {
        if (transactionTable.getValueAt(transactionTable.getSelectedRow(), 3).toString()
                .equalsIgnoreCase("Wypoyczenie")) {
            session = NewHibernateUtil.getSessionFactory().openSession();
            try {

                session.beginTransaction();
                Query query = session
                        .createQuery("update Transakcja set idPracownika =:id where idTransakcji =:idT");
                query.setParameter("id", loggedId);
                query.setParameter("idT", Integer.parseInt(transactionTable.getModel()
                        .getValueAt(transactionTable.getSelectedRow(), 0).toString()));
                query.executeUpdate();/*from  ww w .ja  va  2s.c  o m*/
                session.getTransaction().commit();

                transactionPanel.setVisible(false);

                showTransactions();
                transactionPanel.setVisible(true);

            } catch (HibernateException he) {
                he.printStackTrace();

            }
        }
    }

}

From source file:NewJFrame.java

private void declineTransactionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_declineTransactionButtonActionPerformed
    if (transactionTable.getSelectedRow() != -1) {
        if (transactionTable.getValueAt(transactionTable.getSelectedRow(), 3).toString()
                .equalsIgnoreCase("Wypoyczenie")) {
            session = NewHibernateUtil.getSessionFactory().openSession();
            try {

                session.beginTransaction();
                Query query = session.createQuery("delete Transakcja where idTransakcji =:idT");
                query.setParameter("idT", Integer.parseInt(transactionTable.getModel()
                        .getValueAt(transactionTable.getSelectedRow(), 0).toString()));
                query.executeUpdate();/*from w ww  . ja v a2 s.c o  m*/
                session.getTransaction().commit();

                transactionPanel.setVisible(false);

                showTransactions();
                transactionPanel.setVisible(true);

            } catch (HibernateException he) {
                he.printStackTrace();

            }
        }
    }
}

From source file:NewJFrame.java

private void removeGenreButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeGenreButtonActionPerformed

    int id = Integer
            .parseInt(removeGenreTable.getModel().getValueAt(removeGenreTable.getSelectedRow(), 0).toString());
    try {/*  w w w. j  a va2s  . com*/
        session = NewHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Query query = session.createQuery("delete Gatunek where idGatunku =:id");
        query.setParameter("id", id);
        int result = query.executeUpdate();

        if (result > 0) {
            removeGenreNotify.setText("Usunito gatunek!");
            removeGenreNotify.setForeground(Color.green);
            removeGenreNotify.setVisible(true);
        }

    } catch (HibernateException he) {
        removeGenreNotify.setText("Nie mona usun gatunku!");
        removeGenreNotify.setForeground(Color.red);
        removeGenreNotify.setVisible(true);
    } finally {
        session.getTransaction().commit();
        session.close();
        reloadRemoveGenresTable();

    }

}

From source file:GetMovDetails.java

protected int getDetails(String movname) {
    Configuration configuration = new Configuration();
    configuration.configure();// w  w w.  ja v  a  2  s  . c om
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    Session session = new Configuration().configure().buildSessionFactory(serviceRegistry).openSession();

    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        String hql = "FROM GS_Movie m where m.name=:movname";
        Query query = session.createQuery(hql);
        query.setParameter("movname", movname);
        List results = query.list();
        GS_Movie gS_Movie = (GS_Movie) results.iterator().next();
        name = gS_Movie.getName();
        movid = gS_Movie.getMovid();
        plot = gS_Movie.getPlot();
        year = String.valueOf(gS_Movie.getYear());
        dir = gS_Movie.getDirector();
        genre = gS_Movie.getGenre();
        prod = gS_Movie.getProducer();
        cast = gS_Movie.getCast();
        //Update mov details
        System.out.println(name);
        System.out.println(movid);
        System.out.println(plot);
        System.out.println(year);
        System.out.println(dir);
        System.out.println(genre);
        System.out.println(prod);
        System.out.println(cast);

        result = 1;

    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
        return result;
    }
}

From source file:Bazica.java

public User getUserByName(String email) {
    Query query = session.createQuery("from User where email=:email");
    query.setParameter("email", email);
    List<User> list = query.list();
    if (list.size() == 0)
        return null;

    return list.get(0);
}

From source file:ac.cr.una.backend.dao.AuthorDaoImpl.java

@Override
public Author findByName(String name) {
    Author autor = null;/*from   w ww. jav a  2 s.  c  om*/
    Query query = session.createQuery("from Author where nombre = :nombre ");//name
    query.setParameter("nombre", name);

    if (query.list().size() > 0) {
        autor = (Author) query.list().get(0);
    }

    return autor;
}

From source file:ac.cr.una.backend.dao.AuthorDAOImple.java

@Override
public Author findByName(String name) {
    Author author = null;/*from   w  w w  .ja  v a2 s.com*/
    Query query = session.createQuery("from Author where name = :name ");
    query.setParameter("name", name);

    if (query.list().size() > 0) {
        author = (Author) query.list().get(0);
    }

    return author;
}

From source file:ac.cr.una.backend.dao.BookTypeDAOImple.java

@Override
public BookType findByName(String name) {
    BookType bookType = null;//from  www . j  av a2  s. com
    Query query = session.createQuery("from Author where name = :name ");
    query.setParameter("name", name);

    if (query.list().size() > 0) {
        bookType = (BookType) query.list().get(0);
    }

    return bookType;
}