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:org.openhie.openempi.dao.hibernate.ReportDaoHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Report> getReports() {
    log.trace("Retrieving all reports from the system.");
    return (List<Report>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            List<Report> reports = (List<Report>) session.createQuery("from Report r order by r.name asc")
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            log.trace("Found " + reports.size() + " reports in the system.");
            for (Report report : reports) {
                session.evict(report);
            }/*from   ww w . j  av a  2s .c o  m*/
            return reports;
        }
    });
}

From source file:org.openhie.openempi.dao.hibernate.ReportDaoHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<ReportRequestEntry> getReportRequests() {
    log.trace("Retrieving all reports from the system.");
    return (List<ReportRequestEntry>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            List<ReportRequestEntry> reports = (List<ReportRequestEntry>) session
                    .createQuery("from ReportRequestEntry r order by r.dateRequested desc")
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                    .setMaxResults(maxReportRequestsReturned).list();
            log.trace("Found " + reports.size() + " report requests in the system.");
            for (ReportRequestEntry entry : reports) {
                session.evict(entry);
            }/*from w w w.  j av  a2  s. com*/
            return reports;
        }
    });
}

From source file:org.openmrs.module.dhisreport.api.db.hibernate.HibernateDHIS2ReportingDAO.java

License:Open Source License

public Identifiable saveObject(Identifiable object) {
    System.out.println("inside save object==============");
    Session session = sessionFactory.getCurrentSession();
    // force merge if uid already exists
    System.out.println(object.getUid() + "====" + object.getClass());
    Identifiable existingObject = getObjectByUid(object.getUid(), object.getClass());
    if (existingObject != null) {
        session.evict(existingObject);
        System.out.println("existing object====" + existingObject.getId());
        object.setId(existingObject.getId());
        session.load(object, object.getId());
    }/*from  w w  w  . j a  v  a2  s.  c  o m*/
    sessionFactory.getCurrentSession().saveOrUpdate(object);
    return object;
}

From source file:org.opentox.toxotis.persistence.db.RegisterTool.java

License:Open Source License

public void storeAlgorithm(Algorithm algorithm) {
    Session session = getSession();
    try {/*from ww w  .  j a  v a  2s .  co  m*/
        session.beginTransaction();
        preprocessComponent(algorithm);
        for (Parameter p : algorithm.getParameters()) {
            session.saveOrUpdate(p);
            session.flush();
            session.evict(p);
        }
        session.saveOrUpdate(algorithm);
        session.flush();
        session.evict(algorithm);
        session.getTransaction().commit();
        session.clear();
    } catch (RuntimeException ex) {
        logger.warn("Storage of User failed. Logging the corresponding exception for details", ex);
        try {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }
}

From source file:org.opentox.toxotis.persistence.db.RegisterTool.java

License:Open Source License

public void storeModel(Model model) {
    Session session = getSession();
    Transaction transaction = null;// w  w  w .j av a 2  s .co m
    session.setFlushMode(FlushMode.COMMIT);
    try {
        preprocessComponent(model);
        transaction = session.beginTransaction();

        session.evict(model.getAlgorithm());

        if (model.getParameters() != null) {
            for (Parameter p : model.getParameters()) {
                session.saveOrUpdate(p);
                session.flush();
                session.evict(p);
            }
        }

        if (model.getIndependentFeatures() != null) {
            for (Feature ft : model.getIndependentFeatures()) {
                storeFeature(ft);
                session.evict(ft);
            }
        }

        if (model.getCreatedBy() != null) {
            session.saveOrUpdate(model.getCreatedBy());
            session.flush();
            session.evict(model.getCreatedBy());
        }
        if (model.getDependentFeatures() != null) {
            for (Feature depFeature : model.getDependentFeatures()) {
                session.saveOrUpdate(depFeature);
                session.flush();
                session.evict(depFeature);
            }
        }

        if (model.getPredictedFeatures() != null) {
            for (Feature predictedFeature : model.getPredictedFeatures()) {
                session.saveOrUpdate(predictedFeature);
                session.flush();
                session.evict(predictedFeature);
            }
        }

        session.saveOrUpdate(model);
        transaction.commit();
        session.clear();
    } catch (StaleObjectStateException ex) {
        logger.error("Serious exception that cannot be recovered! Stale Object!!!!");
        try {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } catch (RuntimeException ex) {
        logger.warn("Storage of Model failed. Logging the corresponding exception for details", ex);
        try {
            if (transaction != null) {
                transaction.rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }
}

From source file:org.opentox.toxotis.persistence.db.RegisterTool.java

License:Open Source License

public void storeDataset(Dataset dataset) {
    Session session = getSession();
    try {//w  w w .  jav a 2  s .co  m
        preprocessComponent(dataset);
        session.beginTransaction();
        for (Feature f : dataset.getContainedFeatures()) {
            session.saveOrUpdate(f);
            session.flush();
            session.evict(f);
        }
        session.getTransaction().commit();
        session.clear();

        session.beginTransaction();
        Set<FeatureValue> ff = dataset.getFeatureValues();
        for (FeatureValue p : ff) {
            session.saveOrUpdate(p);
            session.flush();
            session.evict(p);
        }
        session.saveOrUpdate(dataset);
        session.getTransaction().commit();
        session.clear();
    } catch (StaleObjectStateException ex) {
        logger.error("Serious exception that cannot be recovered! Stale Object!!!!");
        try {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } catch (RuntimeException ex) {
        logger.warn("Storage of User failed. Logging the corresponding exception for details", ex);
        try {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }
}

From source file:org.opentox.toxotis.persistence.db.RegisterTool.java

License:Open Source License

public synchronized void storeTask(Task task) {
    Session session = getSession();
    Transaction transaction = null;//from  w w w. java  2  s. co  m
    session.setFlushMode(FlushMode.COMMIT);

    try {
        transaction = session.beginTransaction();
        User createdBy = task.getCreatedBy();
        if (createdBy != null) {
            session.saveOrUpdate(createdBy);
            session.flush();
            session.evict(createdBy);
        }
        ErrorReport er = task.getErrorReport();
        if (er != null) {
            session.saveOrUpdate(er);
            session.flush();
            session.evict(er);
        }
        session.saveOrUpdate(task);
        session.flush();
        transaction.commit();

    } catch (RuntimeException ex) {
        logger.warn("Storage of Task failed. Logging the corresponding exception for details.", ex);
        try {
            if (transaction != null) {
                transaction.rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }
}

From source file:org.openvpms.component.business.dao.hibernate.im.archetype.ArchetypeDescriptorDOTestCase.java

License:Open Source License

/**
 * Verifies that an {@link AssertionDescriptorDOImpl}'s ProperyMap is correctly
 * loaded when its other attributes are null.
 *//*from   w  w  w . j  a v a  2  s.  c o m*/
@Test
public void testOBF112() {
    AssertionDescriptorDO assertion = new AssertionDescriptorDOImpl();
    assertion.setName("assertionOBF112");
    assertion.addProperty(createAssertionProperty("expression", String.class, ".*"));
    assertNotNull(assertion.getPropertyMap());

    Session session = getSession();
    Transaction tx = session.beginTransaction();
    session.save(assertion);
    tx.commit();
    session.evict(assertion); // evict the assertion to force load from db

    // reload and verify the property map was loaded
    AssertionDescriptorDO loaded = (AssertionDescriptorDO) session.load(AssertionDescriptorDOImpl.class,
            assertion.getId());
    PropertyMap map = loaded.getPropertyMap();
    assertNotNull(map);
    assertNotNull(loaded.getProperty("expression"));
}

From source file:org.openvpms.component.business.dao.hibernate.im.party.PartyDOTestCase.java

License:Open Source License

/**
 * Tests that a simple PartyDO can be made persistent.
 * <p/>//from   ww  w  .  ja  v  a 2s. co  m
 * Verifies that properties in the associated 'details' table is removed
 * when the PartyDO is deleted.
 */
@Test
public void testPartySaveDelete() {
    Session session = getSession();

    // get initial count of parties and corresponding details

    PartyDO person = createPerson();
    int size = person.getDetails().size();
    assertTrue(size != 0);

    Transaction tx = session.beginTransaction();
    session.save(person);
    tx.commit();

    // reload
    session.evict(person);
    person = session.load(PartyDOImpl.class, person.getId());
    assertEquals(size, person.getDetails().size());

    // check row count
    assertEquals(parties + 1, count(PartyDOImpl.class));
    assertEquals(details + size, countDetails(PartyDOImpl.class));

    // delete it and verify it no long exists
    tx = session.beginTransaction();
    session.delete(person);
    tx.commit();

    assertNull(session.get(PartyDOImpl.class, person.getId()));
    assertEquals(parties, count(PartyDOImpl.class));
    assertEquals(details, countDetails(PartyDOImpl.class));
}

From source file:org.openvpms.component.business.dao.hibernate.im.party.PartyDOTestCase.java

License:Open Source License

/**
 * Test the removal of parties with entity relationships.
 *///from  w  w w .j ava 2 s.c o m
@Test
public void testEntityRelationshipRemoval() {
    Session session = getSession();
    Transaction tx = session.beginTransaction();

    // create two parties, and add two relationships between each
    PartyDO source = createPerson();
    PartyDO target = createPerson();
    session.saveOrUpdate(source);
    session.saveOrUpdate(target);
    tx.commit();

    tx = session.beginTransaction();
    EntityRelationshipDO rel1 = createEntityRelationship(source, target);
    EntityRelationshipDO rel2 = createEntityRelationship(source, target);
    session.saveOrUpdate(source);
    session.saveOrUpdate(target);

    tx.commit();

    // check the relationship count
    assertEquals(relationships + 2, count(EntityRelationshipDOImpl.class));

    // now remove the first relationship
    tx = session.beginTransaction();

    source.removeSourceEntityRelationship(rel1);
    target.removeTargetEntityRelationship(rel1);
    session.saveOrUpdate(source);
    session.saveOrUpdate(target);

    tx.commit();

    // check the count
    assertEquals(relationships + 1, count(EntityRelationshipDOImpl.class));
    assertEquals(1, target.getTargetEntityRelationships().size());

    // now delete the target. The remaining entity relationship should
    // also be deleted
    session.evict(source);
    tx = session.beginTransaction();
    session.delete(target);
    tx.commit();

    assertNull(session.get(EntityRelationshipDOImpl.class, rel2.getId()));

    // check the count
    assertEquals(relationships, count(EntityRelationshipDOImpl.class));
}