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.wso2.appserver.sample.chad.data.ChadPersistenceManager.java

License:Apache License

/**
 * Retrieve a chad poll//from ww  w  .j a va  2s .  co  m
 *
 * @param pollId
 * @return The chad poll with uuid <code>pollId</code>
 */
public ChadPoll getPoll(String pollId) {
    Session session = hbConfig.currentSession();
    Transaction tx = session.beginTransaction();
    ChadPoll poll = null;
    try {
        Criteria criteria = session.createCriteria(ChadPoll.class);
        criteria.add(Expression.eq("pollId", pollId.trim()));
        poll = (ChadPoll) criteria.uniqueResult();
        session.evict(poll);
        tx.commit();
    } catch (Throwable t) {
        t.printStackTrace();
        tx.rollback();
    }
    return poll;
}

From source file:ubic.gemma.persistence.service.association.Gene2GeneProteinAssociationDaoImpl.java

License:Apache License

@Override
public void thaw(Gene2GeneProteinAssociation gene2GeneProteinAssociation) {
    if (gene2GeneProteinAssociation == null)
        return;/*  w ww.  j a v  a 2s . c  o m*/
    if (gene2GeneProteinAssociation.getId() == null)
        return;

    Session session = this.getSessionFactory().getCurrentSession();

    EntityUtils.attach(session, gene2GeneProteinAssociation, Gene2GeneProteinAssociationImpl.class,
            gene2GeneProteinAssociation.getId());
    Hibernate.initialize(gene2GeneProteinAssociation);
    Hibernate.initialize(gene2GeneProteinAssociation.getFirstGene());
    Hibernate.initialize(gene2GeneProteinAssociation.getSecondGene());

    if (gene2GeneProteinAssociation.getSecondGene().getTaxon() != null
            && gene2GeneProteinAssociation.getSecondGene().getTaxon().getId() != null) {
        Hibernate.initialize(gene2GeneProteinAssociation.getSecondGene().getTaxon());
        Hibernate.initialize(gene2GeneProteinAssociation.getFirstGene().getTaxon());
    }

    session.evict(gene2GeneProteinAssociation);

}

From source file:ubic.gemma.persistence.service.expression.arrayDesign.ArrayDesignDaoImpl.java

License:Apache License

@Override
public void removeBiologicalCharacteristics(final ArrayDesign arrayDesign) {
    if (arrayDesign == null) {
        throw new IllegalArgumentException("Array design cannot be null");
    }//from  ww  w. jav  a2  s.com
    Session session = this.getSessionFactory().getCurrentSession();
    session.buildLockRequest(LockOptions.NONE).lock(arrayDesign);

    int count = 0;
    for (CompositeSequence cs : arrayDesign.getCompositeSequences()) {
        cs.setBiologicalCharacteristic(null);
        session.update(cs);
        session.evict(cs);
        if (++count % ArrayDesignDaoImpl.LOGGING_UPDATE_EVENT_COUNT == 0) {
            AbstractDao.log.info("Cleared sequence association for " + count + " composite sequences");
        }
    }

}

From source file:ubic.gemma.persistence.service.expression.bioAssayData.DesignElementDataVectorDaoImpl.java

License:Apache License

@Override
public void thawRawAndProcessed(Collection<DesignElementDataVector> designElementDataVectors) {
    if (designElementDataVectors == null)
        return;/*ww w .ja va 2 s. co m*/

    Session session = this.getSessionFactory().getCurrentSession();

    Hibernate.initialize(designElementDataVectors);

    StopWatch timer = new StopWatch();
    timer.start();
    Collection<ExpressionExperiment> ees = new HashSet<>();
    Map<BioAssayDimension, Collection<DesignElementDataVector>> dims = new HashMap<>();
    Collection<CompositeSequence> cs = new HashSet<>();
    for (DesignElementDataVector vector : designElementDataVectors) {
        session.buildLockRequest(LockOptions.NONE).lock(vector);
        Hibernate.initialize(vector);
        Hibernate.initialize(vector.getQuantitationType());

        BioAssayDimension bad = vector.getBioAssayDimension();
        if (!dims.containsKey(bad)) {
            dims.put(bad, new HashSet<DesignElementDataVector>());
        }

        dims.get(bad).add(vector);
        cs.add(vector.getDesignElement());
        ees.add(vector.getExpressionExperiment());
        session.evict(vector.getQuantitationType());
        session.evict(vector);
    }

    if (timer.getTime() > designElementDataVectors.size()) {
        AbstractDao.log.info("Thaw phase 1, " + designElementDataVectors.size() + " vectors initialized in "
                + timer.getTime() + "ms ");
    }
    timer.reset();
    timer.start();

    // lightly thaw the EEs we saw
    for (ExpressionExperiment ee : ees) {
        Hibernate.initialize(ee);
        session.evict(ee);
    }

    if (timer.getTime() > 200) {
        AbstractDao.log.info("Thaw phase 2, " + ees.size() + " vector-associated expression experiments in "
                + timer.getTime() + "ms ");
    }

    timer.reset();
    timer.start();

    // thaw the bioassayDimensions we saw -- usually one, more rarely two.
    for (BioAssayDimension bad : dims.keySet()) {

        BioAssayDimension tbad = (BioAssayDimension) this.getSessionFactory().getCurrentSession().createQuery(
                "select distinct bad from BioAssayDimension bad join fetch bad.bioAssays ba join fetch ba.sampleUsed "
                        + "bm join fetch ba.arrayDesignUsed left join fetch bm.factorValues fetch all properties where bad.id= :bad ")
                .setParameter("bad", bad.getId()).uniqueResult();

        assert tbad != null;
        assert !dims.get(tbad).isEmpty();

        for (DesignElementDataVector v : designElementDataVectors) {
            if (v.getBioAssayDimension().getId().equals(tbad.getId())) {
                v.setBioAssayDimension(tbad);
            }
        }
    }

    if (timer.getTime() > 1000) {
        AbstractDao.log.info("Thaw phase 3, " + dims.size() + " vector-associated bioassaydimensions in "
                + timer.getTime() + "ms ");
    }
    timer.reset();
    timer.start();

    // thaw the designelements we saw. SLOW
    long lastTime = 0;
    int count = 0;
    for (CompositeSequence de : cs) {
        BioSequence seq = de.getBiologicalCharacteristic();
        if (seq == null)
            continue;
        session.buildLockRequest(LockOptions.NONE).lock(seq);
        Hibernate.initialize(seq);

        // is this really necessary?
        ArrayDesign arrayDesign = de.getArrayDesign();
        Hibernate.initialize(arrayDesign);

        if (++count % 10000 == 0) {
            if (timer.getTime() - lastTime > 1000) {
                AbstractDao.log
                        .info("Thawed " + count + " vector-associated probes " + timer.getTime() + " ms");
            }
            lastTime = timer.getTime();
        }
    }

    timer.stop();
    if (designElementDataVectors.size() >= 2000 || timer.getTime() > 200) {
        AbstractDao.log.info(
                "Thaw phase 4 " + cs.size() + " vector-associated probes thawed in " + timer.getTime() + "ms");
    }
}

From source file:ubic.gemma.persistence.service.expression.designElement.CompositeSequenceDaoImpl.java

License:Apache License

@Override
public void thaw(final Collection<CompositeSequence> compositeSequences) {
    HibernateTemplate templ = this.getHibernateTemplate();
    templ.executeWithNativeSession(new org.springframework.orm.hibernate3.HibernateCallback<Object>() {
        @Override// w  ww.j  av  a2s.  c o  m
        public Object doInHibernate(org.hibernate.Session session) throws org.hibernate.HibernateException {
            int i = 0;
            int numToDo = compositeSequences.size();
            for (CompositeSequence cs : compositeSequences) {

                session.buildLockRequest(LockOptions.NONE).lock(cs);
                Hibernate.initialize(cs.getArrayDesign());
                session.buildLockRequest(LockOptions.NONE).lock(cs.getArrayDesign());
                Hibernate.initialize(cs.getArrayDesign().getPrimaryTaxon());

                BioSequence bs = cs.getBiologicalCharacteristic();
                if (bs == null) {
                    continue;
                }

                session.buildLockRequest(LockOptions.NONE).lock(bs);
                Hibernate.initialize(bs);
                Hibernate.initialize(bs.getTaxon());

                DatabaseEntry dbEntry = bs.getSequenceDatabaseEntry();
                if (dbEntry != null) {
                    Hibernate.initialize(dbEntry);
                    Hibernate.initialize(dbEntry.getExternalDatabase());
                    session.evict(dbEntry);
                    session.evict(dbEntry.getExternalDatabase());
                }

                if (bs.getBioSequence2GeneProduct() == null) {
                    continue;
                }

                for (BioSequence2GeneProduct bs2gp : bs.getBioSequence2GeneProduct()) {
                    if (bs2gp == null) {
                        continue;
                    }
                    GeneProduct geneProduct = bs2gp.getGeneProduct();
                    if (geneProduct != null && geneProduct.getGene() != null) {
                        Gene g = geneProduct.getGene();
                        g.getAliases().size();
                        session.evict(g);
                        session.evict(geneProduct);
                    }

                }

                if (++i % 2000 == 0) {
                    AbstractDao.log.info("Progress: " + i + "/" + numToDo + "...");
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        //
                    }
                }

                session.evict(bs);
            }
            session.clear();
            return null;
        }
    });

}

From source file:ubic.gemma.persistence.service.genome.sequenceAnalysis.BlatAssociationDaoImpl.java

License:Apache License

@Override
public void thaw(final Collection<BlatAssociation> blatAssociations) {
    if (blatAssociations == null)
        return;//from  ww w  . ja  v  a2s . c  o m
    HibernateTemplate templ = this.getHibernateTemplate();
    templ.executeWithNativeSession(new org.springframework.orm.hibernate3.HibernateCallback<Object>() {
        @Override
        public Object doInHibernate(org.hibernate.Session session) throws org.hibernate.HibernateException {
            for (Object object : blatAssociations) {
                BlatAssociation blatAssociation = (BlatAssociation) object;
                if ((blatAssociation).getId() == null)
                    continue;
                BlatAssociationDaoImpl.this.thawBlatAssociation(session, blatAssociation);
                session.evict(blatAssociation);
            }
            return null;
        }

    });
}

From source file:ubic.gemma.persistence.util.EntityUtils.java

License:Apache License

/**
 * Expert only. Put the given entity into the Session, with LockMode.NONE
 * Based on idea from https://forum.hibernate.org/viewtopic.php?p=2284826#p2284826
 *
 * @param session Hibernate Session (use factory.getCurrentSession())
 * @param obj     the entity/*from   w  ww.  j a  v  a2  s.c  o m*/
 * @param clazz   the class type of the persisted entity. Don't use obj.getClass() as this might return a proxy type.
 * @param id      identifier of the obj
 */
public static void attach(Session session, Object obj, Class<?> clazz, Long id) {
    if (obj == null || id == null)
        return;
    if (!session.isOpen())
        throw new IllegalArgumentException("Illegal attempt to use a closed session");
    if (!session.contains(obj)) {

        Object oldObj = session.get(clazz, id);

        if (oldObj != null) {
            session.evict(oldObj);
        }
    }
    session.buildLockRequest(LockOptions.NONE).lock(obj);
}

From source file:uk.ac.ox.it.ords.api.database.services.impl.hibernate.DatabaseUploadServiceImpl.java

License:Apache License

@Override
public void setImportProgress(int id, ImportType progress) throws Exception {

    OrdsPhysicalDatabase physicalDatabase = this.getPhysicalDatabaseFromID(id);
    Session session = this.getOrdsDBSessionFactory().openSession();
    try {//  www  . j ava 2 s . com
        Transaction transaction = session.beginTransaction();
        session.evict(physicalDatabase);
        physicalDatabase.setImportProgress(progress);
        session.update(physicalDatabase);
        session.flush();
        transaction.commit();
    } catch (Exception e) {
        log.debug(e.getMessage());
        session.getTransaction().rollback();
        throw e;
    } finally {
        session.close();
    }
}

From source file:uk.org.rbc1b.roms.db.HibernatePersonChangeDao.java

License:Open Source License

@Override
public Person getOldPerson(Integer personId, Person person) {
    Session session = this.sessionFactory.openSession();
    session.evict(person);
    Person oldPerson = (Person) session.get(Person.class, personId);
    session.close();//ww  w . j ava 2 s .  co m
    return oldPerson;
}