Example usage for org.hibernate Session refresh

List of usage examples for org.hibernate Session refresh

Introduction

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

Prototype

void refresh(Object object);

Source Link

Document

Re-read the state of the given instance from the underlying database.

Usage

From source file:it.eng.spagobi.commons.dao.RoleDAOHibImpl.java

License:Mozilla Public License

/**
 * Erase role./*  ww  w  .j a v  a2  s.  co  m*/
 * 
 * @param aRole the a role
 * 
 * @throws EMFUserError the EMF user error
 * 
 * @see it.eng.spagobi.commons.dao.IRoleDAO#eraseRole(it.eng.spagobi.commons.bo.Role)
 */
public void eraseRole(Role aRole) throws EMFUserError {
    Session aSession = null;
    Transaction tx = null;
    try {
        aSession = getSession();
        tx = aSession.beginTransaction();

        SbiExtRoles hibRole = (SbiExtRoles) aSession.load(SbiExtRoles.class, aRole.getId());
        // deletes associations with events (and events themselves, if they have no more associations)
        //Query hibQuery = aSession.createQuery(" from SbiEventRole ser where ser.id.role.extRoleId = " + hibRole.getExtRoleId().toString());
        Query hibQuery = aSession.createQuery(" from SbiEventRole ser where ser.id.role.extRoleId = ?");
        hibQuery.setInteger(0, hibRole.getExtRoleId().intValue());
        List eventsRole = hibQuery.list();
        Iterator it = eventsRole.iterator();
        while (it.hasNext()) {
            SbiEventRole eventRole = (SbiEventRole) it.next();
            SbiEventsLog event = eventRole.getId().getEvent();
            aSession.delete(eventRole);
            aSession.flush();
            aSession.refresh(event);
            Set roles = event.getRoles();
            if (roles.isEmpty()) {
                aSession.delete(event);
            }
        }
        aSession.delete(hibRole);
        tx.commit();
    } catch (HibernateException he) {
        logException(he);

        if (tx != null)
            tx.rollback();

        throw new EMFUserError(EMFErrorSeverity.ERROR, 100);

    } finally {
        if (aSession != null) {
            if (aSession.isOpen())
                aSession.close();
        }
    }

}

From source file:itensil.security.hibernate.UserEntity.java

License:Open Source License

private void connectUser() {
    Session session = HibernateUtil.getSession();
    if (!session.contains(this)) {
        session.refresh(this);
    }//from ww w  .  j  a  v  a 2  s  .  co  m
}

From source file:itensil.workflow.activities.WFItemJunit.java

License:Open Source License

@SuppressWarnings("unchecked")
public void testUserItem() throws Exception {
    Session normSess = HibernateUtil.getSession();
    String usr1Id = "junitUsr1";
    String usr2Id = "junitUsr2";
    String flowId = "flow1";

    HibernateUtil.beginTransaction();// w  ww . j a v  a  2 s .co  m
    FlowState fState = new FlowState();
    fState.setId(flowId);
    fState.setActive(true);

    normSess.saveOrUpdate(fState);

    Activity item1 = new Activity(idGen.createID().toString());
    item1.setFlow(fState);
    //item1.setId(normSess.getIdentifier());
    item1.setName("Activity One");

    item1.setSubmitId(usr2Id);
    ActivityStepState state = new ActivityStepState();
    state.setAssignId(usr1Id);
    state.setActivity(item1);
    state.setStepId("My Step");
    item1.getStates().put(state.getStepId(), state);

    Activity subItem1 = new Activity(idGen.createID().toString());
    subItem1.setFlow(fState);
    subItem1.setName("Sub Activity One");

    subItem1.setSubmitId(usr2Id);
    subItem1.setParent(item1);
    ActivityStepState state2 = new ActivityStepState();
    state2.setAssignId(usr1Id);
    state2.setActivity(subItem1);
    state2.setStepId("My Other Step");
    subItem1.getStates().put(state2.getStepId(), state2);

    //item1.getChildren().add(subItem1);
    normSess.persist(item1);
    normSess.persist(subItem1);
    normSess.persist(state);
    normSess.persist(state2);

    //normSess.persist(state);

    HibernateUtil.commitTransaction();

    HibernateUtil.beginTransaction();
    normSess = HibernateUtil.getSession();
    normSess.refresh(item1);
    assertEquals(1, item1.getChildren().size());
    HibernateUtil.commitTransaction();

    HibernateUtil.beginTransaction();
    normSess = HibernateUtil.getSession();

    Activity act1 = (Activity) normSess.get(Activity.class, item1.getId());
    System.out.println(ActivityXML.display(act1));

    act1 = (Activity) normSess.get(Activity.class, subItem1.getId());
    System.out.println(ActivityXML.display(act1));

    System.out.println("--getAssignItems--");

    UserActivities usrItms = new UserActivities(new DefaultUser(usr1Id), normSess);
    List<Activity> asgnElems = usrItms.getAssignActivities(0, false);
    for (Activity aEl : asgnElems) {
        System.out.println(ActivityXML.display(aEl));
    }

    System.out.println("--getSubmitItems--");
    usrItms = new UserActivities(new DefaultUser(usr2Id), normSess);
    List<Activity> subtElems = usrItms.getSubmitActivities(0, false);
    for (Activity sEl : subtElems) {
        System.out.println(ActivityXML.display(sEl));
    }

    HibernateUtil.closeSession();
}

From source file:jmm.persist.DaoImpl.java

License:Open Source License

/**
 * (non-Javadoc)/*from   ww  w. j a  v  a 2s  . c o  m*/
 * 
 * @see architecture.hibernate.DataAccess#save(java.lang.Object)
 * also starts a transaction, if none is active
 */
@Override
public boolean save(T obj) {
    Session s = dbAccess.getActiveSession();
    try {
        s.saveOrUpdate(obj);
        return true;
    } catch (StaleObjectStateException stex) {
        s.refresh(obj);
        return false;
    } catch (RuntimeException rex) {
        Transaction t = s.getTransaction();
        if (t != null) {
            t.rollback();
        }
        s.close();
        throw rex;
    }
}

From source file:models.db.acentera.impl.DAO.java

License:Open Source License

public static MetaData refresh(MetaData meta) throws DAOException {
    Session s = (Session) HibernateSessionFactory.getSession();
    s.refresh(meta);
    return meta;//from w  w  w.jav  a2  s . c o m
}

From source file:mx.edu.um.mateo.colportor.dao.UnionDao.java

License:Open Source License

public Union crea(Union union, Usuario usuario) {
    Session session = currentSession();
    session.save(union);/*w w  w.j a  v a 2s.c o  m*/
    Asociacion asociacion = new Asociacion("Asociacion del Noreste", Constantes.STATUS_ACTIVO, union);
    if (usuario != null) {
        usuario.setAsociacion(asociacion);
    }
    asociacionDao.crea(asociacion, usuario);
    reporteDao.inicializaUnion(union);
    session.refresh(union);
    session.flush();
    return union;
}

From source file:mx.edu.um.mateo.colportor.dao.UnionDao.java

License:Open Source License

public Union actualiza(Union union, Usuario usuario) {
    Session session = currentSession();
    log.debug("NombreCompleto: {}", union.getNombre());
    session.update(union);/*from w w  w  .j a  v  a 2 s  . com*/
    session.merge(union);
    session.flush();
    if (usuario != null) {
        session.refresh(union);
        actualizaUsuario: for (Asociacion asociacion : union.getAsociaciones()) {
            usuario.setAsociacion(asociacion);
            session.update(usuario);
            break actualizaUsuario;
        }
    }

    session.flush();
    return union;
}

From source file:mx.edu.um.mateo.general.dao.EmpresaDao.java

License:Open Source License

public Empresa crea(Empresa empresa, Usuario usuario) {
    Session session = currentSession();
    session.save(empresa);/*from w  w w .  ja  v a 2  s.c om*/
    Almacen almacen = new Almacen("CENTRAL", empresa);
    session.save(almacen);
    if (usuario != null) {
        usuario.setEmpresa(empresa);
        usuario.setAlmacen(almacen);
        session.update(usuario);
    }
    session.refresh(empresa);
    return empresa;
}

From source file:mx.edu.um.mateo.general.dao.impl.EmpresaDaoHibernate.java

License:Open Source License

@Override
public Empresa crea(Empresa empresa, Usuario usuario) {
    Session session = currentSession();
    if (usuario != null) {
        empresa.setOrganizacion(usuario.getEmpresa().getOrganizacion());
    }/* w  ww . jav  a  2 s  .  co m*/
    session.save(empresa);
    Almacen almacen = new Almacen("CT", "CENTRAL", empresa);
    almacen = almacenDao.crea(almacen, usuario);
    if (usuario != null) {
        usuario.setEmpresa(empresa);
        usuario.setAlmacen(almacen);
        session.update(usuario);
    }
    Proveedor proveedor = new Proveedor(empresa.getNombre(), empresa.getNombreCompleto(), empresa.getRfc(),
            true, empresa);
    session.save(proveedor);
    TipoCliente tipoCliente = new TipoCliente("TIPO1", "TIPO1", empresa);
    session.save(tipoCliente);
    Cliente cliente = new Cliente(empresa.getNombre(), empresa.getNombreCompleto(), empresa.getRfc(),
            tipoCliente, true, empresa);
    session.save(cliente);
    reporteDao.inicializaEmpresa(empresa);
    session.refresh(empresa);
    return empresa;
}

From source file:mx.edu.um.mateo.general.dao.impl.OrganizacionDaoHibernate.java

License:Open Source License

@Override
public Organizacion crea(Organizacion organizacion, Usuario usuario) {
    Session session = currentSession();
    session.save(organizacion);//from   w ww .  ja  v a 2s  . c o  m
    Calendar cal = Calendar.getInstance();
    StringBuilder idEjercicio = new StringBuilder();
    idEjercicio.append("001-");
    idEjercicio.append(cal.get(Calendar.YEAR));
    EjercicioPK ejercicioPK = new EjercicioPK(idEjercicio.toString(), organizacion);
    Byte x = new Byte("0");
    Ejercicio ejercicio = new Ejercicio(ejercicioPK, idEjercicio.toString(), "A", StringUtils.EMPTY,
            StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, x, x);
    session.save(ejercicio);
    log.debug("Ejercicio creado {}", ejercicio);
    Empresa empresa = new Empresa("MTZ", "MATRIZ", "MATRIZ", "000000000001", organizacion);
    if (usuario != null) {
        usuario.setEmpresa(empresa);
    }
    empresaDao.crea(empresa, usuario);
    reporteDao.inicializaOrganizacion(organizacion);
    session.refresh(organizacion);
    session.flush();
    return organizacion;
}