Example usage for org.hibernate Session evict

List of usage examples for org.hibernate Session evict

Introduction

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

Prototype

void evict(Object object);

Source Link

Document

Remove this instance from the session cache.

Usage

From source file:demo.repository.UserRepositoryTest.java

License:Apache License

private void saveFlushEvict(Object... entities) {
    Session session = sessionFactory.getCurrentSession();
    for (Object entity : entities) {
        session.save(entity);// w  w  w  .  j a v a 2s .  c o  m
    }
    session.flush();
    for (Object entity : entities) {
        session.evict(entity);
    }
}

From source file:edu.scripps.fl.pubchem.PubChemDB.java

License:Apache License

public static void saveAssay(Session session, PCAssay assay) {
    PCAssay storedAssay = null;/*from   ww  w. ja v  a2 s  .com*/
    if (assay.getAID() > 0) {
        Query query = session.createQuery("from PCAssay where assay_aid = " + assay.getAID());
        storedAssay = (PCAssay) query.uniqueResult();
    }
    if (storedAssay != null) {

        if (storedAssay.getVersion() != assay.getVersion() && storedAssay.getRevision() != assay.getRevision())
            assay.setVersionChanged(true);

        delete(session, storedAssay.getAssayXRefs());
        delete(session, storedAssay.getColumns());
        delete(session, storedAssay.getPanels());
        storedAssay.getCategorizedComments().clear();
        session.save(storedAssay);
        session.flush();

        for (PCAssayPanel panel : assay.getPanels()) {
            panel.setAssay(storedAssay);
            storedAssay.getPanels().add(panel);
            session.save(panel);
        }
        for (PCAssayXRef assayXRef : assay.getAssayXRefs()) {
            assayXRef.setAssay(storedAssay);
            storedAssay.getAssayXRefs().add(assayXRef);
            session.save(assayXRef);
        }
        for (PCAssayColumn col : assay.getColumns()) {
            col.setAssay(storedAssay);
            storedAssay.getColumns().add(col);
            session.save(col);
        }
        assay.setId(storedAssay.getId());
        session.evict(storedAssay);
        session.update(assay);
    } else {
        assay.setVersionChanged(true);
        session.save(assay);
        for (PCAssayPanel panel : assay.getPanels()) {
            session.save(panel);
        }
        for (PCAssayXRef assayXRef : assay.getAssayXRefs()) {
            session.save(assayXRef);
        }
        for (PCAssayColumn col : assay.getColumns()) {
            session.save(col);
        }
    }
}

From source file:eionet.webq.dao.KnownHostsTest.java

License:Mozilla Public License

private void evict(KnownHost knownHost) {
    Session currentSession = sessionFactory.getCurrentSession();
    currentSession.flush();
    currentSession.evict(knownHost);
}

From source file:eionet.webq.dao.ProjectFileStorageImplTest.java

License:Mozilla Public License

@Test(expected = LazyInitializationException.class)
public void allFilesQueryDoesNotReturnFileContent() throws Exception {
    addOneFile("fileName1");
    Session currentSession = sessionFactory.getCurrentSession();
    currentSession.clear();/*from w w w .  j av a  2 s  . com*/

    Collection<ProjectFile> projectFiles = projectFileStorage.findAllFilesFor(projectEntry);
    assertThat(projectFiles.size(), equalTo(1));
    ProjectFile file = projectFiles.iterator().next();
    currentSession.evict(file);

    file.getFileContent();
}

From source file:eionet.webq.dao.UserFileStorageImplTest.java

License:Mozilla Public License

@Test(expected = LazyInitializationException.class)
public void filesContentIsFetchedLazily() throws Exception {
    storage.save(fileWithContentAndXmlSchema("test-content".getBytes()), userId);
    Session currentSession = sessionFactory.getCurrentSession();
    currentSession.clear();/*from  w ww . j av  a 2  s  . c om*/

    UserFile theOnlyFile = getFirstUploadedFileAndAssertThatItIsTheOnlyOneAvailableFor(userId);
    currentSession.evict(theOnlyFile);

    theOnlyFile.getContent();// content must be not initialized
}

From source file:Employee.EmpDAO.java

public boolean update(int oid) {
    Session session = HibernateUtil.getSessionFactory();
    Transaction transaction = session.beginTransaction();
    EmpDAO e = (EmpDAO) session.get(EmpDAO.class, this.empID);
    session.evict(e);
    e.setEmpGender(this.empGender);
    e.setEmpID(this.getEmpID());
    e.setEmpName(this.empName);
    e.setEmpSalary(this.empSalary);
    session.update(e);// w  w w.  ja  v a  2s. c  o m
    transaction.commit();
    return true;
}

From source file:es.logongas.ix3.dao.impl.GenericDAOImplHibernate.java

License:Apache License

@Override
final public EntityType readOriginalByNaturalKey(DataSession dataSession, Object naturalKey)
        throws BusinessException {
    Session session = (Session) dataSession.getDataBaseSessionAlternativeImpl();
    try {/*from  w  ww.  j a  v a 2  s.  co  m*/
        session.setCacheMode(CacheMode.IGNORE);
        EntityType entity = (EntityType) session.bySimpleNaturalId(getEntityMetaData().getType())
                .load(naturalKey);
        if (entity != null) {
            session.evict(entity);
        }
        return entity;
    } catch (javax.validation.ConstraintViolationException cve) {
        throw new BusinessException(exceptionTranslator.getBusinessMessages(cve));
    } catch (org.hibernate.exception.ConstraintViolationException cve) {
        throw new BusinessException(exceptionTranslator.getBusinessMessages(cve));
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

}

From source file:es.logongas.ix3.dao.impl.GenericDAOImplHibernate.java

License:Apache License

@Override
final public EntityType readOriginal(DataSession dataSession, PrimaryKeyType id) throws BusinessException {
    Session session = (Session) dataSession.getDataBaseSessionAlternativeImpl();
    try {// w w  w .  j  ava  2s. c  o m
        session.setCacheMode(CacheMode.IGNORE);
        EntityType entity = (EntityType) session.get(getEntityMetaData().getType(), id);
        if (entity != null) {
            session.evict(entity);
        }
        return entity;
    } catch (javax.validation.ConstraintViolationException cve) {
        throw new BusinessException(exceptionTranslator.getBusinessMessages(cve));
    } catch (org.hibernate.exception.ConstraintViolationException cve) {
        throw new BusinessException(exceptionTranslator.getBusinessMessages(cve));
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:fr.gael.dhus.database.dao.ActionRecordWritterDao.java

License:Open Source License

public void cleanupOlderActionRecords(final int keep_period) {
    if (inactive) {
        logger.warn("Action record access has been deactivated by user via "
                + "\"action.record.inactive\" parameter: the action record tables " + "will not be purged.");
        return;/*ww w . j  a v  a 2s.  com*/
    }

    getHibernateTemplate().execute(new HibernateCallback<Void>() {
        @Override
        public Void doInHibernate(Session session) throws HibernateException, SQLException {
            long days = keep_period * 24 * 60 * 60 * 1000L;
            Date date = new Date(System.currentTimeMillis() - days);
            String pattern = "DELETE FROM <table> WHERE created < ?";

            String hql = pattern.replace("<table>", "ActionRecordDownload");
            Query query = session.createQuery(hql);
            query.setDate(0, date);
            query.executeUpdate();

            hql = pattern.replace("<table>", "ActionRecordLogon");
            query = session.createQuery(hql);
            query.setDate(0, date);
            query.executeUpdate();

            hql = pattern.replace("<table>", "ActionRecordSearch");
            query = session.createQuery(hql);
            query.setDate(0, date);
            query.executeUpdate();

            hql = "FROM ActionRecordUpload WHERE created < ?";
            query = session.createQuery(hql);
            query.setDate(0, date);

            @SuppressWarnings("unchecked")
            List<ActionRecordUpload> uploads = query.list();
            for (ActionRecordUpload upload : uploads) {
                session.evict(upload);
                session.delete(upload);
            }
            return null;
        }
    });
}

From source file:gr.interamerican.bo2.impl.open.hibernate.AbstractHibernateDetachStrategy.java

License:Open Source License

/**
 * Detaches the entity./*from  w ww. ja va  2 s  .  co  m*/
 * 
 * @param object
 * @param session
 * @return Whether it was indeed detached.
 */
private boolean detachTransientObjectAttachedByCascade(Object object, Session session) {
    boolean evicted = false;
    boolean mdfCleared = false;

    if (session.contains(object)) {
        session.evict(object);
        evicted = true;
    }

    //an object may have been evicted by cascade previously, so we check this separately
    if (object instanceof ModificationRecord) {
        ModificationRecord mdf = (ModificationRecord) object;
        if (mdf.getLastModified() != null) {
            mdf.setLastModified(null);
            mdfCleared = true;
        }
    }

    return evicted || mdfCleared;
}