Example usage for org.hibernate Session get

List of usage examples for org.hibernate Session get

Introduction

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

Prototype

Object get(String entityName, Serializable id);

Source Link

Document

Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.

Usage

From source file:SortedMapJUnitTest.java

@Test(expected = TypeMismatchException.class)
public void TestReadCarException01() {

    Session session = sessionFctry.openSession();
    Transaction tx = null;/*from  w  w w.ja v a  2 s .  co  m*/
    tx = session.beginTransaction();
    Car car = (Car) session.get(Car.class, "this should be id but isn't >:D");

    assertNotNull(car);

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

From source file:SortedMapJUnitTest.java

@Test(expected = IllegalArgumentException.class)
public void TestReadCarException02() {

    Session session = sessionFctry.openSession();
    Transaction tx = null;/*from w  ww.  j  a  v  a2 s  .  c  o m*/
    tx = session.beginTransaction();
    Car car = (Car) session.get(Car.class, null);

    assertNotNull(car);

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

From source file:SortedMapJUnitTest.java

@Test
public void updateCar() {
    /*zakadamy e w bazie isntnieje samochd o id 1 */
    final Integer carIDToTest = 2;

    int random_year_production = (new Random()).nextInt(100) + 1900;
    Session session = sessionFctry.openSession();
    Transaction tx = null;//from   w  w  w  . j  ava2 s.c o  m
    try {
        tx = session.beginTransaction();
        Car car = (Car) session.get(Car.class, carIDToTest);

        assertNotNull(car); //do aktualizacji musi istnie w bazie obiekt o id 1 

        car.setProduction_year(random_year_production);
        session.update(car);

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

    /* sprawdzenie czy obiekt si zaktualizowa */
    session = sessionFctry.openSession();
    int readed_production_year = 0;
    try {
        tx = session.beginTransaction();
        Car car = (Car) session.get(Car.class, carIDToTest);

        assertNotNull(car); //do aktualizacji musi istnie w bazie obiekt o id 1 

        readed_production_year = car.getProduction_year();

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

    assertEquals(random_year_production, readed_production_year);
}

From source file:SortedMapJUnitTest.java

@Test(expected = StaleStateException.class)
public void testUpdateCarException01() {
    /*zakadamy e w bazie isntnieje samochd o id 1 */
    final Integer carIDToTest = 2;

    int random_year_production = (new Random()).nextInt(100) + 1900;
    Session session = sessionFctry.openSession();
    Transaction tx = null;/*  w  ww  .  j a  va  2s  .c o  m*/
    tx = session.beginTransaction();
    Car car = new Car("Opel", "Corsa Mk2", 1998, 3700, "Diesel 2.0", new TreeMap());
    session.update(car);

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

    /* sprawdzenie czy obiekt si zaktualizowa */
    session = sessionFctry.openSession();
    int readed_production_year = 0;
    tx = session.beginTransaction();
    car = (Car) session.get(Car.class, carIDToTest);

    assertNotNull(car); //do aktualizacji musi istnie w bazie obiekt o id 1 

    readed_production_year = car.getProduction_year();

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

    assertEquals(random_year_production, readed_production_year);
}

From source file:SortedMapJUnitTest.java

@Test(expected = IllegalArgumentException.class)
public void testUpdateCarException02() {
    /*zakadamy e w bazie isntnieje samochd o id 1 */
    final Integer carIDToTest = 2;

    int random_year_production = (new Random()).nextInt(100) + 1900;
    Session session = sessionFctry.openSession();
    Transaction tx = null;//w  ww.jav a 2  s  .c  om
    tx = session.beginTransaction();
    Car car = (Car) session.get(Car.class, carIDToTest);

    assertNotNull(car); //do aktualizacji musi istnie w bazie obiekt o id 1 

    car.setProduction_year(random_year_production);
    session.update(null);

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

    /* sprawdzenie czy obiekt si zaktualizowa */
    session = sessionFctry.openSession();
    int readed_production_year = 0;
    tx = session.beginTransaction();
    car = (Car) session.get(Car.class, carIDToTest);

    assertNotNull(car); //do aktualizacji musi istnie w bazie obiekt o id 1 

    readed_production_year = car.getProduction_year();

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

    assertEquals(random_year_production, readed_production_year);
}

From source file:SortedMapJUnitTest.java

@Test
public void deleteCar() {
    //tworzymy obiekt
    Session session = sessionFctry.openSession();
    Transaction tx = null;// w w  w . j  a  v  a 2 s .c  o m
    Integer returned_id = null;

    try {
        tx = session.beginTransaction();

        TreeMap carAdditions = new TreeMap();

        carAdditions.put("Skrzana kierownica", new CarAddition("MVC239Kierownica"));
        carAdditions.put("Skrzane siedzenia", new CarAddition("Cw3GDSSiedzenia"));
        carAdditions.put("Podgrzewane lusterka", new CarAddition("OPEL23432 lustra"));
        carAdditions.put("Chromowane felgi SkullCar 321", new CarAddition("SKULLCAR321"));

        Car newCar = new Car("Opel", "Corsa Mk2", 1998, 3700, "Diesel 2.0", carAdditions);

        returned_id = (Integer) session.save(newCar);

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

    //usuwamy obiekt
    session = sessionFctry.openSession();
    try {
        tx = session.beginTransaction();

        Car car = (Car) session.get(Car.class, returned_id);
        assertNotNull(car); //sprawdzamy czy nie pusty (bo nie powinien by pusty!)
        session.delete(car);

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

    //sprawdzamy czy zosta usunity z bazy
    session = sessionFctry.openSession();
    try {
        tx = session.beginTransaction();

        Car car = (Car) session.get(Car.class, returned_id);
        assertNull(car); //powinno by puste poniewa usunelimy wczeniej ten obiekt

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

}

From source file:SortedMapJUnitTest.java

@Test(expected = IllegalArgumentException.class)
public void testDeleteCarException01() {
    //tworzymy obiekt
    Session session = sessionFctry.openSession();
    Transaction tx = null;/*from  w  ww.  jav a 2s  .com*/
    Integer returned_id = null;

    tx = session.beginTransaction();

    TreeMap carAdditions = new TreeMap();

    carAdditions.put("Skrzana kierownica", new CarAddition("MVC239Kierownica"));
    carAdditions.put("Skrzane siedzenia", new CarAddition("Cw3GDSSiedzenia"));
    carAdditions.put("Podgrzewane lusterka", new CarAddition("OPEL23432 lustra"));
    carAdditions.put("Chromowane felgi SkullCar 321", new CarAddition("SKULLCAR321"));

    Car newCar = new Car("Opel", "Corsa Mk2", 1998, 3700, "Diesel 2.0", carAdditions);

    returned_id = (Integer) session.save(newCar);

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

    //usuwamy obiekt
    session = sessionFctry.openSession();
    tx = session.beginTransaction();

    Car car = (Car) session.get(Car.class, returned_id);
    assertNotNull(car); //sprawdzamy czy nie pusty (bo nie powinien by pusty!)
    session.delete(null);

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

    //sprawdzamy czy zosta usunity z bazy
    session = sessionFctry.openSession();
    tx = session.beginTransaction();

    car = (Car) session.get(Car.class, returned_id);
    assertNull(car); //powinno by puste poniewa usunelimy wczeniej ten obiekt

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

From source file:SortedMapJUnitTest.java

@Test(expected = MappingException.class)
public void testDeleteCarException02() {
    //tworzymy obiekt
    Session session = sessionFctry.openSession();
    Transaction tx = null;// w ww  . j a v  a 2s  .c  om
    Integer returned_id = null;

    tx = session.beginTransaction();

    TreeMap carAdditions = new TreeMap();

    carAdditions.put("Skrzana kierownica", new CarAddition("MVC239Kierownica"));
    carAdditions.put("Skrzane siedzenia", new CarAddition("Cw3GDSSiedzenia"));
    carAdditions.put("Podgrzewane lusterka", new CarAddition("OPEL23432 lustra"));
    carAdditions.put("Chromowane felgi SkullCar 321", new CarAddition("SKULLCAR321"));

    Car newCar = new Car("Opel", "Corsa Mk2", 1998, 3700, "Diesel 2.0", carAdditions);

    returned_id = (Integer) session.save(newCar);

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

    //usuwamy obiekt
    session = sessionFctry.openSession();
    tx = session.beginTransaction();

    String asd = new String("sas");
    session.delete(asd);

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

    //sprawdzamy czy zosta usunity z bazy
    session = sessionFctry.openSession();
    tx = session.beginTransaction();

    Car car = (Car) session.get(Car.class, returned_id);
    assertNull(car); //powinno by puste poniewa usunelimy wczeniej ten obiekt

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

From source file:TestCustomer.java

@Test
public void saveCustomer() {
    Configuration config = new Configuration();
    SessionFactory factory = config.configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = factory.openSession();
    Transaction tran = session.beginTransaction();
    Customer entity = new Customer();
    entity.setId(0);/*from  w  w w.  j  a v a  2s  .  c om*/
    entity.setCustomerType(CustomerType.P);
    Customer data = (Customer) session.get(Customer.class, entity.getId());
    Customer result = (Customer) session.merge(entity);
    tran.commit();
    System.out.print(result.getId());
}

From source file:formEditar.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w w  w .  j av  a2  s. co m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");

        try {

            Session sessao = HibernateUtil.getSessionFactory().openSession();
            String nome = request.getParameter("nome");
            Professor prof = (Professor) sessao.get(Professor.class, nome);

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet formEditar</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<form action=\"editarProfessor\">");
            out.println("Nome: <input type=\"text\" name=\"nome\" value=\"" + prof.getNome()
                    + "\" readonly=\"readonly\" > <br/>");
            out.println("Nome completo: <input type=\"text\" name=\"sobrenome\" value=\"" + prof.getSobrenome()
                    + "\"><br/>");
            out.println("Disciplina <input type=\"text\" name=\"disciplina\"  value=\"" + prof.getDisciplina()
                    + "\"> <br/>");
            out.println("<input type=\"submit\">");
            out.println("</form>");
            out.println("</html>");
            out.println("</body>");
            out.println("</html>");
        } catch (Exception e) {
            out.println("Erro ao gerar form: " + e.getMessage());
            e.printStackTrace();
        }
    }
}