Example usage for org.hibernate Session createQuery

List of usage examples for org.hibernate Session createQuery

Introduction

In this page you can find the example usage for org.hibernate Session createQuery.

Prototype

@Override
    org.hibernate.query.Query createQuery(CriteriaDelete deleteQuery);

Source Link

Usage

From source file:DataBaseCredentialHandler.java

@Override
public HashMap Odczyt() {
    HashMap<String, User> passmap = new HashMap<String, User>();
    Session session = factory.openSession();
    Transaction tx = null;//from   w ww.  j a v  a2  s.co m
    try {
        tx = session.beginTransaction();
        List<User> list = session.createQuery("FROM User").list();

        for (User i : list)
            passmap.put(i.getCredentials().getLogin(), i);

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

From source file:createAccount.java

private static List list() {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();
    List userList = session.createQuery("FROM SystemUser").list();
    session.close();/*from   w w  w  .  j  a va2 s. c om*/
    return userList;
}

From source file:TestingHibernate.java

@Test
public void testIfAnyShowsExist() {
    Session session = Hibernate.getSessionFactory().openSession();
    session.beginTransaction();//from   w  w w. ja  v  a  2 s .c o  m
    List result = session.createQuery("from Shows").setMaxResults(3).list();
    assert (result.size() > 2);
    session.getTransaction().commit();
    session.close();
}

From source file:TestingHibernate.java

@Test
public void testIfAnySongsExist() {
    Session session = Hibernate.getSessionFactory().openSession();
    session.beginTransaction();//  w  ww .  j  av  a2s  .c  o  m
    List result = session.createQuery("from Songs").setMaxResults(3).list();
    //this huge size only works with lazy as true
    assert (result.size() > 2);
    session.getTransaction().commit();
    session.close();
}

From source file:TestingHibernate.java

@Test
public void testIfShowAndSongHaveTheSameID() {
    Session session = Hibernate.getSessionFactory().openSession();
    session.beginTransaction();/*from w  w w . j a  v  a 2  s.com*/
    List<Shows> result = session.createQuery("from Shows").setMaxResults(5).list();
    assert (result.size() == 5);
    Shows show = result.get(1);
    assertNotEquals(show.getId(), show.getSetList().iterator().next().getId());
    session.getTransaction().commit();
    session.close();
}

From source file:Storage.java

License:Open Source License

public static Anime findAnime(String name) {
    Session session = factory.openSession();
    Transaction tx = null;/*from  w ww .j  av  a2s .  c o  m*/
    try {

        tx = session.beginTransaction();
        Anime anime = (Anime) session.createQuery("FROM Anime WHERE name = '" + name + "'").uniqueResult();
        tx.commit();
        return anime;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
        return null;
    } finally {
        session.close();
    }
}

From source file:Storage.java

License:Open Source License

public static Episode findEpisode(String number, Anime anime) {
    Session session = factory.openSession();
    Transaction tx = null;/*from   ww  w  . j  a v  a  2s.c  o m*/
    try {
        tx = session.beginTransaction();
        Episode episode = (Episode) session
                .createQuery(
                        "FROM Episode WHERE anime_id = '" + anime.getId() + "' AND number = '" + number + "'")
                .uniqueResult();
        tx.commit();
        return episode;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
        return null;
    } finally {
        session.close();
    }
}

From source file:Storage.java

License:Open Source License

public static Genre findGenre(String name) {
    Session session = factory.openSession();
    Transaction tx = null;/*from   w ww  .j  ava  2s .  co  m*/
    try {
        tx = session.beginTransaction();
        Genre genre = (Genre) session.createQuery("FROM Genre WHERE name = '" + name + "'").uniqueResult();
        tx.commit();
        return genre;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        return null;
    } finally {
        session.close();
    }
}

From source file:Storage.java

License:Open Source License

public static Source findSource(String url) {
    Session session = factory.openSession();
    Transaction tx = null;//from w  w w. ja  va 2  s. co m
    try {
        tx = session.beginTransaction();
        Source source = (Source) session.createQuery("FROM Source WHERE url = '" + url + "'").uniqueResult();
        tx.commit();
        return source;
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
        return null;
    } finally {
        session.close();
    }
}

From source file:Testing.java

private static void test_juani_connection() {
    SessionFactory factory = Sesion.init();

    Session session = factory.getCurrentSession();

    try {/*from w  ww  .  jav a2  s  .  co  m*/
        session.beginTransaction();

        List<Persona> personas = session.createQuery("from Persona").list();

        for (Persona p : personas) {
            System.out.println(p);
        }

        session.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        Sesion.close();
    }
}