Example usage for org.hibernate Session clear

List of usage examples for org.hibernate Session clear

Introduction

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

Prototype

void clear();

Source Link

Document

Completely clear the session.

Usage

From source file:com.evolveum.midpoint.repo.sql.OrgStructTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test//  w w  w.  j  av  a2s  . c  o m
public void test002modifyOrgStructAddRef() throws Exception {
    LOGGER.info("===[ modifyOrgStruct ]===");
    OperationResult opResult = new OperationResult("===[ modifyOrgStruct ]===");
    // test modification of org ref in another org type..

    ObjectModificationType modification = PrismTestUtil.parseAtomicValue(new File(MODIFY_ORG_ADD_REF_FILENAME),
            ObjectModificationType.COMPLEX_TYPE);
    ObjectDelta<OrgType> delta = DeltaConvertor.createObjectDelta(modification, OrgType.class, prismContext);

    Session session = getFactory().openSession();
    session.beginTransaction();

    Criteria criteria = session.createCriteria(ROrgClosure.class).createCriteria("descendant", "desc")
            .setFetchMode("descendant", FetchMode.JOIN)
            .add(Restrictions.eq("desc.oid", MODIFY_ORG_ADD_REF_OID));

    List<ROrgClosure> orgClosure = criteria.list();

    LOGGER.info("before modify");
    for (ROrgClosure c : orgClosure) {
        LOGGER.info("{}\t{}\t{}",
                new Object[] { c.getAncestor().getOid(), c.getDescendant().getOid(), c.getDepth() });
    }
    AssertJUnit.assertEquals(3, orgClosure.size());
    session.getTransaction().commit();

    repositoryService.modifyObject(OrgType.class, MODIFY_ORG_ADD_REF_OID, delta.getModifications(), opResult);

    session.clear();
    session.beginTransaction();
    criteria = session.createCriteria(ROrgClosure.class).createCriteria("descendant", "desc")
            .setFetchMode("descendant", FetchMode.JOIN)
            .add(Restrictions.eq("desc.oid", MODIFY_ORG_ADD_REF_OID));

    orgClosure = criteria.list();

    LOGGER.info("after modify");
    for (ROrgClosure c : orgClosure) {
        LOGGER.info("{}\t{}\t{}",
                new Object[] { c.getAncestor().getOid(), c.getDescendant().getOid(), c.getDepth() });
    }
    AssertJUnit.assertEquals(4, orgClosure.size());

    List<String> ancestors = new ArrayList<String>();
    ancestors.add(MODIFY_ORG_ADD_REF_OID);
    ancestors.add(ORG_F003_OID);
    ancestors.add(ORG_F001_OID);
    ancestors.add(ORG_F002_OID);

    for (String ancestorOid : ancestors) {
        criteria = session.createCriteria(ROrgClosure.class);
        criteria.createCriteria("ancestor", "anc").setFetchMode("ancestor", FetchMode.JOIN)
                .add(Restrictions.eq("anc.oid", ancestorOid));
        criteria.createCriteria("descendant", "desc").setFetchMode("descendant", FetchMode.JOIN)
                .add(Restrictions.eq("desc.oid", MODIFY_ORG_ADD_REF_OID));

        orgClosure = criteria.list();

        LOGGER.info("=> A: {}, D: {}, depth: {}",
                new Object[] { orgClosure.get(0).getAncestor().toJAXB(prismContext, null),
                        orgClosure.get(0).getDescendant().toJAXB(prismContext, null),
                        orgClosure.get(0).getDepth() });

        AssertJUnit.assertEquals(1, orgClosure.size());
        AssertJUnit.assertEquals(ancestorOid, orgClosure.get(0).getAncestor().getOid());
        AssertJUnit.assertEquals(MODIFY_ORG_ADD_REF_OID, orgClosure.get(0).getDescendant().getOid());
        int depth = -1;
        if (ancestorOid.equals(MODIFY_ORG_ADD_REF_OID)) {
            depth = 0;
        } else if (ancestorOid.equals(ORG_F001_OID)) {
            depth = 2;
        } else if (ancestorOid.equals(ORG_F003_OID)) {
            depth = 1;
        } else if (ancestorOid.equals(ORG_F002_OID)) {
            depth = 1;
        }
        AssertJUnit.assertEquals(depth, orgClosure.get(0).getDepth());

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

From source file:com.evolveum.midpoint.repo.sql.OrgStructTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test//from  w  w w  .  ja  v a 2 s  .  co  m
public void test003modifyOrgStructDeleteRef() throws Exception {

    // test modification of org ref - delete org ref
    LOGGER.info("===[ modify delete org ref ]===");
    OperationResult opResult = new OperationResult("===[ modify delete org ref ]===");

    ObjectModificationType modification = PrismTestUtil
            .parseAtomicValue(new File(MODIFY_ORG_DELETE_REF_FILENAME), ObjectModificationType.COMPLEX_TYPE);

    ObjectDelta<OrgType> delta = DeltaConvertor.createObjectDelta(modification, OrgType.class, prismContext);

    Session session = getFactory().openSession();
    session.beginTransaction();

    LOGGER.info("==>before modify - delete<==");

    Query qDelete = session.createQuery("from ROrgClosure as o where "
            + "o.descendantOid = :descendantOid and o.ancestorOid = :ancestorOid and o.depth = :depth");
    qDelete.setParameter("descendantOid", MODIFY_ORG_DELETE_REF_OID);
    qDelete.setParameter("ancestorOid", ORG_F003_OID);
    qDelete.setParameter("depth", 1);

    List<ROrgClosure> orgClosure = qDelete.list();

    AssertJUnit.assertEquals(1, orgClosure.size());

    session.getTransaction().commit();

    repositoryService.modifyObject(OrgType.class, MODIFY_ORG_DELETE_REF_OID, delta.getModifications(),
            opResult);

    session.clear();
    session.beginTransaction();

    LOGGER.info("==>after modify - delete<==");

    qDelete = session.createQuery("from ROrgClosure as o where "
            + "o.descendantOid = :descendantOid  and o.ancestorOid = :ancestorOid and o.depth = :depth");
    qDelete.setParameter("descendantOid", MODIFY_ORG_DELETE_REF_OID);
    qDelete.setParameter("ancestorOid", ORG_F003_OID);
    qDelete.setParameter("depth", 1);

    orgClosure = qDelete.list();

    AssertJUnit.assertEquals(0, orgClosure.size());

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

From source file:com.evolveum.midpoint.repo.sql.OrgStructTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test/*from   w w  w .java2  s .  c  o  m*/
public void test003modifyOrgStructDeleteRefIncorrect() throws Exception {

    // test modification of org ref - delete org ref
    LOGGER.info("===[ modify delete org ref ]===");
    OperationResult opResult = new OperationResult("===[ modify delete org ref ]===");
    ObjectModificationType modification = PrismTestUtil.parseAtomicValue(
            new File(MODIFY_ORG_INCORRECT_DELETE_REF_FILENAME), ObjectModificationType.COMPLEX_TYPE);

    ObjectDelta<OrgType> delta = DeltaConvertor.createObjectDelta(modification, OrgType.class, prismContext);

    Session session = getFactory().openSession();
    session.beginTransaction();

    LOGGER.info("==>before modify - delete<==");

    Query qDelete = session.createQuery("from ROrgClosure as o where "
            + "o.descendantOid = :descendantOid  and o.ancestorOid = :ancestorOid");
    qDelete.setParameter("descendantOid", MODIFY_ORG_INCORRECT_DELETE_REF_OID);
    qDelete.setParameter("ancestorOid", ORG_F012_OID);

    List<ROrgClosure> orgClosure = qDelete.list();

    AssertJUnit.assertEquals(0, orgClosure.size());

    session.getTransaction().commit();

    repositoryService.modifyObject(OrgType.class, MODIFY_ORG_INCORRECT_DELETE_REF_OID, delta.getModifications(),
            opResult);

    session.clear();
    session.beginTransaction();

    LOGGER.info("==>after modify - delete<==");

    qDelete = session.createQuery("from ROrgClosure as o where "
            + "o.descendantOid = :descendantOid and o.ancestorOid = :ancestorOid");
    qDelete.setParameter("descendantOid", MODIFY_ORG_INCORRECT_DELETE_REF_OID);
    qDelete.setParameter("ancestorOid", ORG_F012_OID);

    orgClosure = qDelete.list();

    AssertJUnit.assertEquals(0, orgClosure.size());

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

From source file:com.femsa.mkt.dao.CobHistoricoDao.java

public boolean guardarLista(List<MktCobStHisAgrupacion> listaAgrupacion) {
    HibernateUtil hibernateUtil = new HibernateUtil();
    SessionFactory sessionFactory = hibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    boolean flagOk = true;
    long contador = 0L;
    if (listaAgrupacion != null) {
        try {/*from  w w  w  . j a v a 2  s . com*/
            session.beginTransaction();
            for (MktCobStHisAgrupacion mktCobStHisAgrupacion : listaAgrupacion) {
                session.save(mktCobStHisAgrupacion);
                if (contador % 100 == 0) {
                    session.flush();
                    session.clear();
                }
                contador++;
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            flagOk = false;
        } finally {
            session.flush();
            session.clear();
            session.close();
            hibernateUtil.closeSessionFactory();
        }
    }
    return flagOk;
}

From source file:com.femsa.mkt.dao.CobHistoricoDao.java

public boolean boorrarTabla() {
    HibernateUtil hibernateUtil = new HibernateUtil();
    SessionFactory sessionFactory = hibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    boolean flagOk = true;
    Query query = session.createSQLQuery("TRUNCATE TABLE MKT_COB_ST_HIS_AGRUPACION");
    try {//  w  w  w.j  ava 2 s  . co  m
        session.beginTransaction();
        query.executeUpdate();
        session.getTransaction().commit();
    } catch (Exception e) {
        if (session.getTransaction().isActive()) {
            session.getTransaction().rollback();
        }
        flagOk = false;
    } finally {
        session.flush();
        session.clear();
        session.close();
        hibernateUtil.closeSessionFactory();
    }
    return flagOk;

}

From source file:com.femsa.mkt.dao.CobHistoricoDao.java

public boolean EjecutaEtl() {
    HibernateUtil hibernateUtil = new HibernateUtil();
    SessionFactory sessionFactory = hibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    boolean flagOk = true;
    Query query = session.createSQLQuery("TRUNCATE TABLE MKT_COB_ST_HIS_AGRUPACION");
    try {//from w ww. ja  v  a2  s .c  o  m
        session.beginTransaction();
        query.executeUpdate();
        session.getTransaction().commit();
    } catch (Exception e) {
        if (session.getTransaction().isActive()) {
            session.getTransaction().rollback();
        }
        flagOk = false;
    } finally {
        session.flush();
        session.clear();
        session.close();
        hibernateUtil.closeSessionFactory();
    }
    return flagOk;

}

From source file:com.femsa.mkt.dao.CobHistoricoDao_Objetivos.java

public boolean guardarLista(List<MktCobStHisObjetivos> listaAgrupacion) {
    HibernateUtil hibernateUtil = new HibernateUtil();
    SessionFactory sessionFactory = hibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    boolean flagOk = true;
    if (listaAgrupacion != null) {
        try {/*from   w w  w  . j  ava  2 s .  c om*/
            session.beginTransaction();
            for (MktCobStHisObjetivos mktCobStHisObjetivos : listaAgrupacion) {
                session.save(mktCobStHisObjetivos);
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            flagOk = false;
        } finally {
            session.flush();
            session.clear();
            session.close();
            hibernateUtil.closeSessionFactory();
        }
    }
    return flagOk;
}

From source file:com.fiveamsolutions.nci.commons.util.PasswordUserTypeTest.java

License:Open Source License

@Test
public void testIt() throws Exception {
    PasswordUserType put = new PasswordUserType();
    Password p = SecurityUtils.create("test");

    assertEquals(p, put.assemble(p, null));
    assertNull(put.deepCopy(null));//from   w ww  .ja  v a 2 s . c o m
    assertEquals(p, put.deepCopy(p));
    assertEquals(p, put.disassemble(p));
    assertFalse(put.equals(null, null));
    assertTrue(put.equals(p, p));
    assertEquals(p.hashCode(), put.hashCode(p));
    assertTrue(put.isMutable());
    assertEquals(p, put.replace(p, null, null));
    assertEquals(Password.class, put.returnedClass());
    assertEquals(1, put.sqlTypes().length);
    assertEquals(Types.VARCHAR, put.sqlTypes()[0]);

    AbstractUser bu = new DummyUser();
    bu.setUsername("test");
    bu.setPassword(p);
    bu.setEmail("test@example.com");
    bu.setFirstName("Dummy");
    bu.setLastName("User");
    Session s = getCurrentSession();
    s.save(bu);
    s.flush();
    s.clear();

    bu = (AbstractUser) s.get(AbstractUser.class, bu.getId());
    assertNotNull(bu);
    assertTrue(SecurityUtils.matches(bu.getPassword(), "test"));

    PasswordReset pr = PasswordReset.newInstance(bu);
    assertNotNull(pr.getCreateDate());
    assertNotNull(pr.getUser());
    assertNotNull(pr.getNonce());
    assertFalse(bu.getPasswordResets().isEmpty());
}

From source file:com.gisgraphy.domain.repository.GenericDao.java

License:Open Source License

public void flushAndClear() {
    this.getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws PersistenceException {
            session.flush();// ww w.  j a v a 2 s  . c  o  m
            session.clear();
            return null;
        }
    });

}

From source file:com.gisgraphy.domain.repository.GisFeatureDao.java

License:Open Source License

public int deleteAllExceptAdmsAndCountries() {
    return ((Integer) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            String queryString = "delete from " + persistentClass.getSimpleName()
                    + " as g where (g.featureCode NOT IN ('ADM1','ADM2','ADM3','ADM4') OR g.featureCode is null) AND g.featureId NOT IN (select featureId from Country)  ";

            Query qry = session.createQuery(queryString);
            // Need to flush to avoid optimisticLock exception
            session.flush();//from w  w w . j ava 2s. co  m
            session.clear();
            qry.setCacheable(false);

            return Integer.valueOf(qry.executeUpdate());

        }
    })).intValue();

}