Example usage for org.hibernate Session update

List of usage examples for org.hibernate Session update

Introduction

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

Prototype

void update(Object object);

Source Link

Document

Update the persistent instance with the identifier of the given detached instance.

Usage

From source file:bikerent.AddDeleteEntity.java

@Test
public void testAUDEntity() {

    // #############################################
    // ################## A D D ####################
    // #############################################

    System.out.println("TEST INSERT: ");
    System.out.println("3 Customer (3 Personen) hinzufgen ");
    System.out.println("2 Female-, 1 Male-, 1 Child-Bike (4 Bikes) hinzufgen ");
    System.out.println("3 Helmets, 3 Chainlocks, 2 Trailer (8 Accessoires) hinzufgen ");

    Session session = sm.getSession();
    addCustomer(session);/* www  . ja  v a2s.  c om*/
    addBikes(session);
    addAccessiores(session);
    // Committing the change in the database.
    session.flush();
    Transaction tx = session.beginTransaction();
    tx.commit();
    System.out.println(" -------------------------------------------- ");
    System.out.println("Anzahl Bikes: " + countBikes(session));
    System.out.println("Anzahl Person: " + countPerson(session));
    System.out.println("Anzahl Accessoires: " + countAccessoires(session));
    System.out.println("Anzahl Items: " + countItem(session));
    System.out.println(" -------------------------------------------- ");
    assertEquals("Count of bikes must be 4: ", 4, countBikes(session));
    assertEquals("Count of person must be 3: ", 3, countPerson(session));
    assertEquals("Count of accessoires must be 8: ", 8, countAccessoires(session));
    assertEquals("Count of item must be 12: ", 12, countItem(session));

    System.out.println(printBikes(session));

    // #############################################
    // ############### U P D A T E #################
    // #############################################

    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println("TEST UPDATE: ");
    System.out.println("Person mit der ID 1 (Sheldon Cooper) auslesen, umbenennen und wieder auslesen");

    System.out.println(printPerson(session));

    custID = getFirstPersonID(session);
    Object object = session.load(Person.class, custID);
    Person p = (Person) object;
    System.out.println(" -------------------------------------------- ");
    System.out.println("Name: " + p.GetName());

    Transaction tx1 = session.beginTransaction();
    p.SetName("Howard", "Wolowitz");
    session.update(p);
    session.flush();
    tx1.commit();

    object = session.load(Person.class, custID);
    p = (Person) object;
    System.out.println("Neuer Name: " + p.GetName());
    System.out.println(" -------------------------------------------- ");

    System.out.println(printPerson(session));

    object = session.load(Customer.class, custID);
    Customer c = (Customer) object;
    System.out.println("Neuer Name des betroffenen CUSTOMERS (Subklasse): " + c.GetName());
    System.out.println(" -------------------------------------------- ");

    assertEquals("Count of person must be 3: ", 3, countPerson(session));

    // #############################################
    // ############### D E L E T E #################
    // #############################################

    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println("TEST DELETE: ");
    System.out.println("Alle Items lschen und prfen, ob noch Bikes existieren");

    System.out.println(" -------------------------------------------- ");
    System.out.println("Momentane Anzahl an Items: " + countItem(session));
    System.out.println("Momentane Anzahl an Bikes: " + countBikes(session));
    List<Item> iList = session.createQuery("from Item").list();
    Transaction tx2 = session.beginTransaction();
    for (Item i : iList) {
        session.delete(i);
    }
    session.flush();
    tx2.commit();

    System.out.println("Neue Anzahl an Items: " + countBikes(session));
    System.out.println("Neue Anzahl an Bikes: " + countBikes(session));
    System.out.println(" -------------------------------------------- ");

    assertEquals("Count of bikes must be 0: ", 0, countBikes(session));

}

From source file:bikerent.AddDeleteEntity.java

public void UpdateEntity() {

    //Selecting Student Records
    Session session = sm.getSession();
    custID = getFirstPersonID(session);/*www  .  j a  v  a  2 s  .c o  m*/
    Object object = session.load(Person.class, custID);
    Person p = (Person) object;
    System.out.println("Name: " + p.GetName());

    Transaction tx1 = session.beginTransaction();
    p.SetName("Justin", "Timberlake");
    session.update(p);
    session.flush();
    tx1.commit();

    object = session.load(Person.class, custID);
    p = (Person) object;
    System.out.println("Name: " + p.GetName());

    assertEquals("Count of person must be 3: ", 3, countPerson(session));
}

From source file:biomart.DAO.LoginDAO.java

public String updateLoginDetails(LoginBean loginBean) {

    Session session = Util.getSessionFactory().openSession();
    Transaction t = null;/*from  w  ww  . jav a  2s . c  o m*/
    try {
        t = session.beginTransaction();
        session.update(loginBean);
        t.commit();
    } catch (HibernateException e) {
        if (t != null) {
            t.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
    return "success";
}

From source file:br.al.contractmanager.dao.UsuarioDao.java

@Transactional
public boolean atualizar(Usuario usuario) {
    Session session = sessionFactory.getCurrentSession();
    try {/*  www.  ja v a 2s.  c o  m*/
        session.update(usuario);
        return true;
    } catch (HibernateException e) {
        System.out.println(e.getMessage());
    }
    return false;
}

From source file:br.com.alberteije.balcao.ProdutoDetalheController.java

License:Open Source License

@Override
public Response updateRecord(ValueObject oldPersistentObject, ValueObject persistentObject) throws Exception {
    Session session = null;
    try {//from www.  j a  va2  s.  c  o m
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        ProdutoVO produto = (ProdutoVO) persistentObject;

        String sql = "select PRODUTO from br.com.alberteije.vo.ProdutoVO as PRODUTO where PRODUTO.gtin = "
                + produto.getGtin() + " and PRODUTO.id <> " + produto.getId();

        produto = (ProdutoVO) session.createQuery(sql).uniqueResult();
        if (produto != null) {
            return new ErrorResponse("Cdigo de produto j cadastrado!");
        }

        produto = (ProdutoVO) persistentObject;
        produto.setHashIncremento(-1);
        session.update(produto);
        session.getTransaction().commit();
        return new VOResponse(produto);
    } catch (Exception e) {
        e.printStackTrace();
        session.getTransaction().rollback();
        return new ErrorResponse("Erro ao cadastrar o produto!");
    } finally {
        session.close();
    }
}

From source file:br.com.aula.dao.DepartamentoDAO.java

public void alterarDepartamento(Departamento departamento) {
    Session session = factory.openSession();
    Transaction transaction = session.beginTransaction();
    session.update(departamento);
    session.flush();// w  ww  .  j a  va 2  s .  c om
    transaction.commit();
    session.close();
}

From source file:br.com.aula.dao.DependenteDAO.java

public void alterarDependente(Dependente dependente) {
    Session session = factory.openSession();
    Transaction transaction = session.beginTransaction();
    session.update(dependente);
    session.flush();//w w w . ja  v a  2 s .  c  o m
    transaction.commit();
    session.close();
}

From source file:br.com.aula.dao.EmpresaDAO.java

public void alterarEmpresa(Empresa empresa) {
    Session session = factory.openSession();
    Transaction transaction = session.beginTransaction();
    session.update(empresa);
    session.flush();/*from w w  w .jav a 2  s.com*/
    transaction.commit();
    session.close();
}

From source file:br.com.batcommerce.dao.AdministradorDAO.java

public void alterar(Administrador administrador) {
    Session sessao = conexao.openSession();
    Transaction transacao = sessao.beginTransaction();

    sessao.update(administrador);
    transacao.commit();/*from   www. j a va 2 s.  c  o  m*/
    sessao.close();

}

From source file:br.com.batcommerce.dao.ProdutoDAO.java

public void alterar(Produto produto) {
    Session sessao = conexao.openSession();
    Transaction transacao = sessao.beginTransaction();

    sessao.update(produto);
    transacao.commit();/*from   ww w  .j ava  2 s. c  om*/
    sessao.close();

}