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:GetMovDetails.java

protected int getDetails(String movname) {
    Configuration configuration = new Configuration();
    configuration.configure();//from  w w w.j  av 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:TeachingPlanMandatoryTest.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
public void testBasicUsage() {

    Session session = sessionFactory.openSession();
    session.beginTransaction();/*from   w w  w .j a  v a  2  s. co m*/
    session.save(new TeachingPlanMandatory(2, 1, 0, 1, 3, 0, 0, 0, 1, 2, 6, 4, 2, 2, 0, 0, 0, 2, 3, 2, 0));
    session.save(new TeachingPlanMandatory(4, 2, 3, 1, 3, 0, 0, 0, 1, 2, 6, 4, 2, 2, 0, 0, 0, 2, 3, 2, 0));
    session.getTransaction().commit();
    session.close();

    session = sessionFactory.openSession();
    session.beginTransaction();
    List result = session.createQuery("from TeachingPlanMandatory").list();
    for (TeachingPlanMandatory m : (List<TeachingPlanMandatory>) result) {
        System.out.println("Test (" + m.getId() + ") : " + m.getTikyba());
    }
    session.getTransaction().commit();
    session.close();
}

From source file:HibernateCoursework.java

public void listEmployees() {
    System.out.println("List Employees");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();/*  www .  j  a v  a2  s .  co  m*/
    System.out.println("***************");

    List employeeList = hibernate.createQuery("from Employee").list();
    System.out.println(employeeList.size() + " employee(s) found:");
    for (Iterator iter = employeeList.iterator(); iter.hasNext();) {
        //assign employee
        Employee e = (Employee) iter.next();
        System.out.println(e.getName());
    }
    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");
}

From source file:HibernateCoursework.java

public void listCustomers() {
    System.out.println("List Customers");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();//from  w w w . ja v  a2  s. co m
    System.out.println("****************");

    List customerList = hibernate.createQuery("from Customer").list();
    System.out.println(customerList.size() + " customer(s) found:");
    for (Iterator iter = customerList.iterator(); iter.hasNext();) {
        //assign employee
        Customer e = (Customer) iter.next();
        System.out.println(e.getName());
    }
    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");
}

From source file:HibernateCoursework.java

public void listProjects() {
    System.out.println("List Projects");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();//w  w w  .  ja v  a2  s. c  o m
    System.out.println("****************");

    List projectList = hibernate.createQuery("from Project").list();
    System.out.println(projectList.size() + " project(s) found:");
    for (Iterator iter = projectList.iterator(); iter.hasNext();) {
        //assign employee
        Project p = (Project) iter.next();
        System.out.println(p.getTitle());
    }
    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");
}

From source file:HibernateCoursework.java

public void findProj(int projId) {
    System.out.println("Specified Project");

    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();/* w w w . ja  v  a  2s.c o  m*/
    System.out.println("*****************");

    List projectList = hibernate.createQuery("from Project p where p.projId =  " + projId).list();
    System.out.println(projectList.size() + " project(s) found:");
    for (Iterator iter = projectList.iterator(); iter.hasNext();) {
        //assign project
        Project p = (Project) iter.next();
        System.out.println("ProjectID = " + p.getProjId());
        System.out.println("Project Title = " + p.getTitle());
        System.out.println("Project Days = " + p.getDays());
    }
    hibernate.getTransaction().commit();
    hibernate.flush();
    hibernate.close();
    System.out.println("***Session closed***");
}

From source file:HibernateCoursework.java

public Employee findEmployeeNumber(int empNumber) {
    Session hibernate = sessionFactory.openSession();
    hibernate.beginTransaction();//from   w ww  .ja  v  a 2 s .  co  m
    System.out.println("***Session started***");

    List tmpList = hibernate.createQuery("from Employee e where e.empNo =" + empNumber).list();
    Employee emp = null;

    for (Iterator iter = tmpList.iterator(); iter.hasNext();) {
        emp = (Employee) iter.next();
        System.out.println("Employee found" + emp.getName().toUpperCase() + "(" + emp.getEmpNo() + ")");

        hibernate.getTransaction().commit();
        hibernate.flush();
        hibernate.close();
        System.out.println("***Session closed***");
    }
    return emp;
}

From source file:StudentJUnitTest.java

@Test
public void readStudentsFromDB() {
    Session session = sessionFctry.openSession();
    Transaction tx = null;/*from   w w w .  j  a va2 s .  com*/

    try {
        tx = session.beginTransaction();

        List students = session.createQuery("FROM Student").list();

        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {

        session.close();
    }

}

From source file:TestSelectUser.java

@Test
public void selectUsersLOginAndPass() {
    String login = "admin";
    String pass = "admin";

    org.hibernate.Session session = HibernateUtil.getSessionFactory().openSession();
    Query query = session.createQuery(
            "FROM com.segvek.inmovie.entity.User WHERE " + "login='" + login + "' AND pass='" + pass + "'");
    User users = (User) query.uniqueResult();

    System.out.println(users);//from   ww  w. java2 s .co m

}

From source file:DbConnectionJUnitTest.java

@Test(expected = QuerySyntaxException.class)
public void TestReadException01() {
    Session session = sessionFctry.openSession();
    Transaction tx = null;//from   ww  w .j a v a  2  s.co  m

    tx = session.beginTransaction();

    List students = session.createQuery("FROM nonexistingtable").list();

    tx.commit();
    session.close();
}