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:au.org.theark.lims.model.dao.InventoryDao.java

License:Open Source License

public void updateInvSite(LimsVO modelObject) {

    InvSite invSite = modelObject.getInvSite();
    Session session = getSession();
    session.update(invSite);//ww  w .j  av  a2  s . c  o  m
    session.flush();
    session.refresh(invSite);
    List<StudyInvSite> existingInvSites = invSite.getStudyInvSites();

    //for (StudyInvSite sis : modelObject.getInvSite().getStudyInvSites()) {
    //      session.delete(sis);
    //      session.flush();
    //}

    List<Long> selectedAndExistingStudies = new ArrayList<Long>();
    List<Study> selectedStudies = modelObject.getSelectedStudies();

    for (Study selectedStudy : selectedStudies) {
        boolean studyAlreadyLinked = false;
        log.info("selected =" + selectedStudy.getId());

        for (StudyInvSite sis : existingInvSites) {
            Study existingStudy = sis.getStudy();
            log.info("  existing=" + selectedStudy.getId());
            if (existingStudy.getId().equals(selectedStudy.getId())) {
                log.info("found a match for " + selectedStudy.getId());
                studyAlreadyLinked = true;
                selectedAndExistingStudies.add(selectedStudy.getId());
                break; // leave it along
            }
        }

        if (!studyAlreadyLinked) {
            log.info("about to create" + selectedStudy.getId());
            StudyInvSite studyInvSite = new StudyInvSite();
            studyInvSite.setStudy(selectedStudy);
            studyInvSite.setInvSite(invSite);
            session.save(studyInvSite);
        }

    }

    for (StudyInvSite sis : existingInvSites) {
        log.info("about to investigate for deletion existing study " + sis.getStudy().getId());
        boolean deletePreviouslyExistingSiteAsItWasNotSelected = true;
        for (Long selectedId : selectedAndExistingStudies) {
            log.info("compare it to selected " + selectedId);
            if (selectedId.equals(sis.getStudy().getId())) {
                log.info("recommending you don't delete");
                deletePreviouslyExistingSiteAsItWasNotSelected = false;
            } else {
                log.info("match not found.");
            }
        }
        if (deletePreviouslyExistingSiteAsItWasNotSelected) {
            log.info("deleting " + sis.getStudy().getId());
            session.delete(sis);
        }
    }
    session.flush();
    session.refresh(invSite);

    //List<StudyInvSite> existingInvSites = invSite.getStudyInvSites();

    //for (StudyInvSite sis : modelObject.getInvSite().getStudyInvSites()) {
    //   session.delete(sis);
    //   session.flush();
    //}

    /*
            
    List<Study> selectedAndExistingStudies = new ArrayList<Study>();
    List<Study> selectedStudies = modelObject.getSelectedStudies();
            
    for (Study selectedStudy : selectedStudies) {
       boolean studyAlreadyLinked = false;
       for(StudyInvSite sis: existingInvSites){
    Study existingStudy = sis.getStudy();
    if(existingStudy.equals(selectedStudy)){
       studyAlreadyLinked = true;
       selectedAndExistingStudies.add(selectedStudy);
       break; // leave it along
    }
       }
               
       if(!studyAlreadyLinked){
    StudyInvSite studyInvSite = new StudyInvSite();
    studyInvSite.setStudy(selectedStudy);
    studyInvSite.setInvSite(modelObject.getInvSite());
    session.save(studyInvSite);
       }
               
    }
            
    for(StudyInvSite sis: existingInvSites){
       if(!selectedAndExistingStudies.contains(sis.getStudy())){
    session.delete(sis);
       }
    }
    */
}

From source file:au.org.theark.lims.model.dao.InventoryDao.java

License:Open Source License

public void unallocateBox(InvBox invBox) {
    Session session = getSession();
    for (InvCell invCell : invBox.getInvCells()) {
        invCell.setBiospecimen(null);/*from w w w. j  av a 2 s  .c  o  m*/
        session.update(invCell);
    }
    session.refresh(invBox);
}

From source file:au.org.theark.phenotypic.model.dao.PhenotypicDao.java

License:Open Source License

public void processPhenoCollectionsWithTheirDataToInsertBatch(
        List<PhenoDataSetCollection> phenoCollectionsWithTheirDataToInsert, Study study) {
    Session session = getSession();
    //      int count = 0;
    for (PhenoDataSetCollection collectionToInsert : phenoCollectionsWithTheirDataToInsert) {
        //TODO : investigate more efficient way to deal with null parent entity
        Set<PhenoDataSetData> dataToSave = collectionToInsert.getPhenoDataSetData();
        collectionToInsert.setPhenoDataSetData(new HashSet<PhenoDataSetData>());

        session.save(collectionToInsert);
        session.refresh(collectionToInsert);
        for (PhenoDataSetData data : dataToSave) {
            data.setPhenoDataSetCollection(collectionToInsert);
            session.save(data);/*  w ww.  j a  v a 2s  .c  o m*/
        }
    }
    session.flush();
    session.clear();
}

From source file:br.edu.ifnmg.ifad.view.GerenciarTurmaBean.java

License:Apache License

@Override
public void edit(Turma entity) {
    professoresSelecionados = new ArrayList<Professor>();
    try {//  ww w  . ja v  a 2s  .  c om
        super.setEntity(entity);
        doInTransaction(new PersistenceActionWithoutResult() {
            @Override
            public void execute(Session s) throws BusinessException {
                s.refresh(getEntity());
                Hibernate.initialize(getEntity().getProfessorList());
                professoresSelecionados.addAll(getEntity().getProfessorList());
            }
        });
        changeStateToEdit();
    } catch (BusinessException ex) {
        addMessage(getSeverityWarn(), ex.getMessage());
    }
}

From source file:business.AccountDB.java

public static String syncAccount(Account a) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    String msg = "";

    try {//from  w  ww . j  av a  2s . c  om
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.merge(a);
        session.getTransaction().commit();
        session.flush();
        session.refresh(a);
        msg = "Account Synced!";
    } catch (HibernateException e) {
        msg = "Error Syncing Account: " + e.getMessage();
        session.getTransaction().rollback();
    } finally {
        session.close();
    }

    return msg;
}

From source file:business.AccountDB.java

public static String addNewAccount(Account a) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    String msg = "";

    try {/*from  ww  w .  j  a  v a2s  .  c  o  m*/
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.persist(a);
        session.getTransaction().commit();
        session.flush();
        session.refresh(a);
        msg = "Account Created!";
    } catch (HibernateException e) {
        msg = "Error Creating Account: " + e.getMessage();
        session.getTransaction().rollback();
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }

    return msg;
}

From source file:business.ChampionDB.java

public static String addNewChampion(Champion c) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    String msg = "";

    try {/*from www .  j  a va 2  s.co m*/
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.persist(c);
        session.getTransaction().commit();
        session.flush();
        session.refresh(c);
        msg = "Champion Created!";
    } catch (HibernateException e) {
        msg = "Error Creating Champion: " + e.getMessage();
        if (session != null && session.isOpen()) {
            session.getTransaction().rollback();
        }
    } finally {
        session.close();
    }

    return msg;
}

From source file:business.MoveDB.java

public static String addNewMove(Move m) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    String msg = "";

    try {/*from  w  w w.  j av  a  2  s.  c om*/
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.persist(m);
        session.getTransaction().commit();
        session.flush();
        session.refresh(m);
        msg = "Move Added!";
    } catch (HibernateException e) {
        msg = "Error Creating Move: " + e.getMessage();
        if (session != null && session.isOpen()) {
            session.getTransaction().rollback();
        }
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }

    return msg;
}

From source file:business.PostDB.java

public static String addNewPost(Post p) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    String msg = "";

    try {//from  w  w  w . j  a v a  2  s .  c  o m
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.persist(p);
        session.getTransaction().commit();
        session.flush();
        session.refresh(p);
        msg = "Post Added!";
    } catch (HibernateException e) {
        msg = "Error Creating Post: " + e.getMessage();
        session.getTransaction().rollback();
    } finally {
        session.close();
    }

    return msg;
}

From source file:ca.mcgill.cs.swevo.qualyzer.util.HibernateUtil.java

License:Open Source License

/**
 * Refresh an object.//www. j a  v a 2 s  .  c  om
 * @param manager
 * @param object
 */
public static void quietRefresh(HibernateDBManager manager, Object object) {
    Transaction t = null;
    Session session = null;
    try {
        session = manager.openSession();
        t = session.beginTransaction();
        session.refresh(object);
        t.commit();
    } catch (HibernateException e) {
        quietRollback(t);
    } finally {
        quietClose(session);
    }
}