Example usage for org.hibernate Session save

List of usage examples for org.hibernate Session save

Introduction

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

Prototype

Serializable save(Object object);

Source Link

Document

Persist the given transient instance, first assigning a generated identifier.

Usage

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 w  w .  ja  v a  2s . c om*/
        }
    }
    session.flush();
    session.clear();
}

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

License:Open Source License

@Override
public void createPhenoDataSetFieldGroup(PhenoDataSetFieldGroupVO phenoDataSetFieldGroupVO)
        throws EntityExistsException, ArkSystemException {
    PhenoDataSetGroup phenoDataSetGroup = phenoDataSetFieldGroupVO.getPhenoDataSetGroup();
    Session session = getSession();
    session.save(phenoDataSetGroup);
    session.flush();//ww w . j av a2  s.c om
    insertToDispalyAndDeleteFromLinkAndPicked(phenoDataSetFieldGroupVO, phenoDataSetGroup, session);
    log.debug("Saved All PhenoDataSetDisplays for PhenoDataSet Group");
}

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

License:Open Source License

private void insertToDispalyAndDeleteFromLinkAndPicked(PhenoDataSetFieldGroupVO phenoDataSetFieldGroupVO,
        PhenoDataSetGroup phenoDataSetGroup, Session session) {
    //Get the Picked Pheno Dataset categories.
    List<PickedPhenoDataSetCategory> phenoDataSetCategories = phenoDataSetFieldGroupVO
            .getPickedAvailableCategories();
    for (PickedPhenoDataSetCategory pickedPhenoDataSetCategory : phenoDataSetCategories) {
        //Get the Linked Pheno Dataset fields for  PickedPhenoDataSetCategory
        List<LinkPhenoDataSetCategoryField> linkPhenoDataSetCategoryFields = getLinkPhenoDataSetCategoryFieldsForPickedPhenoDataSetCategory(
                pickedPhenoDataSetCategory);
        if (!linkPhenoDataSetCategoryFields.isEmpty()) {
            for (LinkPhenoDataSetCategoryField linkPhenoDataSetCategoryField : linkPhenoDataSetCategoryFields) {
                PhenoDataSetFieldDisplay phenoDataSetFieldDisplay = new PhenoDataSetFieldDisplay();
                phenoDataSetFieldDisplay.setPhenoDataSetGroup(phenoDataSetGroup);
                phenoDataSetFieldDisplay
                        .setPhenoDataSetCategory(pickedPhenoDataSetCategory.getPhenoDataSetCategory());
                if (pickedPhenoDataSetCategory.getParentPickedPhenoDataSetCategory() != null) {
                    phenoDataSetFieldDisplay.setParentPhenoDataSetCategory(pickedPhenoDataSetCategory
                            .getParentPickedPhenoDataSetCategory().getPhenoDataSetCategory());
                }/*  w w w  .j av  a2s  .c om*/
                phenoDataSetFieldDisplay
                        .setPhenoDataSetCategoryOrderNumber(pickedPhenoDataSetCategory.getOrderNumber());
                phenoDataSetFieldDisplay
                        .setPhenoDataSetField(linkPhenoDataSetCategoryField.getPhenoDataSetField());
                phenoDataSetFieldDisplay
                        .setPhenoDataSetFiledOrderNumber(linkPhenoDataSetCategoryField.getOrderNumber());
                session.save(phenoDataSetFieldDisplay);
            }
        } else {
            PhenoDataSetFieldDisplay phenoDataSetFieldDisplay = new PhenoDataSetFieldDisplay();
            phenoDataSetFieldDisplay.setPhenoDataSetGroup(phenoDataSetGroup);
            phenoDataSetFieldDisplay
                    .setPhenoDataSetCategory(pickedPhenoDataSetCategory.getPhenoDataSetCategory());
            if (pickedPhenoDataSetCategory.getParentPickedPhenoDataSetCategory() != null) {
                phenoDataSetFieldDisplay.setParentPhenoDataSetCategory(pickedPhenoDataSetCategory
                        .getParentPickedPhenoDataSetCategory().getPhenoDataSetCategory());
            }
            phenoDataSetFieldDisplay
                    .setPhenoDataSetCategoryOrderNumber(pickedPhenoDataSetCategory.getOrderNumber());
            session.save(phenoDataSetFieldDisplay);
        }
    }
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

public void create(Study study, Collection<ArkModule> selectedModules, Study parentStudy) {
    Session session = getSession();
    session.save(study);
    linkStudyToArkModule(study, selectedModules, session, au.org.theark.core.Constants.MODE_NEW);

    // Update parent study accordingly
    if (parentStudy != null) {
        parentStudy.setParentStudy(parentStudy);
        session.update(parentStudy);//www. j av a  2 s. c o m
    }
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

/**
 * {@inheritDoc}/*from www.  j ava2  s .c  o  m*/
 */
public void processPedigreeBatch(List<LinkSubjectPedigree> parentsToInsert,
        List<LinkSubjectTwin> twinsToInsert) {
    Session session = getSession();
    int count = 0;
    for (LinkSubjectPedigree dataToInsert : parentsToInsert) {
        session.save(dataToInsert);
        count++;
        // based on recommended hibernate practice of <prop key="hibernate.jdbc.batch_size">50</prop>
        if (count % 50 == 0) {
            log.info("\n\n\n\n\n\n\n\n\nflush!!!!!!!!!!!!!!");
            session.flush();
            session.clear();
        }
    }
    count = 0;
    for (LinkSubjectTwin dataToInsert : twinsToInsert) {
        session.save(dataToInsert);
        count++;
        // based on recommended hibernate practice of <prop key="hibernate.jdbc.batch_size">50</prop>
        if (count % 50 == 0) {
            log.info("\n\n\n\n\n\n\n\n\nflush!!!!!!!!!!!!!!");
            session.flush();
            session.clear();
        }
    }
    session.flush();
    session.clear();

}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

/**
 * {@inheritDoc}//from w  w  w.  j a v  a 2s.  co  m
 */
public void processSubjectAttachmentBatch(List<SubjectFile> subjectFiles) {
    Session session = getSession();
    int count = 0;
    for (SubjectFile subjectFile : subjectFiles) {
        session.save(subjectFile);
        count++;
        // based on recommended hibernate practice of <prop key="hibernate.jdbc.batch_size">50</prop>
        if (count % 50 == 0) {
            log.info("\n\n\n\n\n\n\n\n\nflush!!!!!!!!!!!!!!");
            session.flush();
            session.clear();
        }
    }
    session.flush();
    session.clear();

}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

public void create(Study study, ArkUserVO arkUserVo, Collection<ArkModule> selectedModules) {
    Session session = getSession();
    session.save(study);
    linkStudyToArkModule(study, selectedModules, session, au.org.theark.core.Constants.MODE_NEW);

    // Update parent study accordingly
    Study parentStudy = study.getParentStudy();
    if (parentStudy != null) {
        parentStudy.setParentStudy(parentStudy);
        session.update(parentStudy);// www. ja  v a 2s  .c o m
    }

    // Assign user to newly created study
    try {
        iArkAuthorisationService.updateArkUser(arkUserVo);
    } catch (ArkSystemException e) {
        log.error(e.getMessage());
    } catch (EntityNotFoundException e) {
        log.error(e.getMessage());
    }
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

private void linkStudyToArkModule(Study study, Collection<ArkModule> selectedModules, Session session,
        int mode) {
    for (ArkModule arkModule : selectedModules) {
        LinkStudyArkModule linkStudyArkModule = new LinkStudyArkModule();
        linkStudyArkModule.setStudy(study);
        linkStudyArkModule.setArkModule(arkModule);
        if (mode == au.org.theark.core.Constants.MODE_NEW) {
            session.save(linkStudyArkModule);
        } else {/*w ww . ja  v a  2s . co m*/
            session.update(linkStudyArkModule);
        }
    }
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

public void updateStudy(Study study, Collection<ArkModule> selectedApplications)
        throws CannotRemoveArkModuleException {
    Session session = getSession();

    // session.update(study);
    // execute merge method instead of update to avoid org.hibernate.NonUniqueObjectException
    session.merge(study);//  w  w  w. j  ava2s. c o m

    Collection<LinkStudyArkModule> linkStudyArkModulesToAdd = getModulesToAddList(study, selectedApplications);
    // Determine Removal List here
    Collection<LinkStudyArkModule> linkStudyArkModulesToRemove = getModulesToRemoveList(study,
            selectedApplications);

    // Process the Removal of Linked ArkModules for this study
    for (LinkStudyArkModule linkStudyArkModule : linkStudyArkModulesToRemove) {
        session.delete(linkStudyArkModule);
    }
    // Insert the new modules for the Study
    for (LinkStudyArkModule linkStudyArkModule : linkStudyArkModulesToAdd) {
        session.save(linkStudyArkModule);
    }

    // Flush must be the last thing to call. If there is any other code/logic to be added make sure session.flush() is invoked after that.
    session.flush();
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

public void createSubject(SubjectVO subjectVo) throws ArkUniqueException, ArkSubjectInsertException {
    Study study = subjectVo.getLinkSubjectStudy().getStudy();
    if (study.getAutoGenerateSubjectUid()) {
        String subjectUID = getNextGeneratedSubjectUID(study);

        if (isSubjectUIDUnique(subjectUID, study.getId(), "Insert")) {
            subjectVo.getLinkSubjectStudy().setSubjectUID(subjectUID);
        } else {// TODO : maybe a for loop to guard against a manual db insert, or just throw exception and holds someone up from further inserts until
                // investigated why?
            subjectUID = getNextGeneratedSubjectUID(study);
            subjectVo.getLinkSubjectStudy().setSubjectUID(subjectUID);
        }/*w w  w  .  j  a v  a  2 s.  c  o  m*/
    } else {
        if (!isSubjectUIDUnique(subjectVo.getLinkSubjectStudy().getSubjectUID(),
                subjectVo.getLinkSubjectStudy().getStudy().getId(), "Insert")) {
            throw new ArkUniqueException("Subject UID must be unique");
        }
    }

    // Set default foreign key reference
    if (subjectVo.getLinkSubjectStudy().getSubjectStatus() == null
            || StringUtils.isBlank(subjectVo.getLinkSubjectStudy().getSubjectStatus().getName())) {
        SubjectStatus subjectStatus = getSubjectStatusByName("Subject");
        subjectVo.getLinkSubjectStudy().setSubjectStatus(subjectStatus);
    }

    // Set default foreign key reference
    if (subjectVo.getLinkSubjectStudy().getPerson().getTitleType() == null
            || StringUtils.isBlank(subjectVo.getLinkSubjectStudy().getPerson().getTitleType().getName())) {
        TitleType titleType = getTitleType(new Long(0));
        subjectVo.getLinkSubjectStudy().getPerson().setTitleType(titleType);
    }

    // Set default foreign key reference
    if (subjectVo.getLinkSubjectStudy().getPerson().getGenderType() == null
            || StringUtils.isBlank(subjectVo.getLinkSubjectStudy().getPerson().getGenderType().getName())) {
        GenderType genderType = getGenderType(new Long(0));
        subjectVo.getLinkSubjectStudy().getPerson().setGenderType(genderType);
    }

    // Set default foreign key reference
    if (subjectVo.getLinkSubjectStudy().getPerson().getVitalStatus() == null
            || StringUtils.isBlank(subjectVo.getLinkSubjectStudy().getPerson().getVitalStatus().getName())) {
        VitalStatus vitalStatus = getVitalStatus(new Long(0));
        subjectVo.getLinkSubjectStudy().getPerson().setVitalStatus(vitalStatus);
    }

    //log.info("create subject otherIDs:  " + subjectVo.getLinkSubjectStudy().getPerson().getOtherIDs());

    if (subjectVo.getLinkSubjectStudy().getPerson().getOtherIDs() == null
            || subjectVo.getLinkSubjectStudy().getPerson().getOtherIDs().isEmpty()) { // get
                                                                                                                                                            // better
                                                                                                                                                            // values
                                                                                                                                                            // when
                                                                                                                                                            // implemented
                                                                                                                                                            // properly
        List<OtherID> otherIDs = new ArrayList<OtherID>();
        subjectVo.getLinkSubjectStudy().getPerson().setOtherIDs(otherIDs);
    }

    Session session = getSession();
    Person person = subjectVo.getLinkSubjectStudy().getPerson();

    if (person.getId() == null) {
        session.save(person);
        PersonLastnameHistory personLastNameHistory = null;

        // Previous LastName (if supplied on new Subject)
        if (subjectVo.getSubjectPreviousLastname() != null
                && !subjectVo.getSubjectPreviousLastname().isEmpty()) {
            personLastNameHistory = new PersonLastnameHistory();
            personLastNameHistory.setPerson(person);
            personLastNameHistory.setLastName(subjectVo.getSubjectPreviousLastname());
            session.save(personLastNameHistory);
        }

        /*
         * only keeping previous names from now on // Current lastName if (person.getLastName() != null) { personLastNameHistory = new
         * PersonLastnameHistory(); personLastNameHistory.setPerson(person); personLastNameHistory.setLastName(person.getLastName());
         * session.save(personLastNameHistory); }
         */
    }

    // Update subjectPreviousLastname TODO investigate
    subjectVo.setSubjectPreviousLastname(getPreviousLastname(person));

    LinkSubjectStudy linkSubjectStudy = subjectVo.getLinkSubjectStudy();
    session.save(linkSubjectStudy);// The hibernate session is the same. This should be automatically bound with Spring's

    autoConsentLinkSubjectStudy(subjectVo.getLinkSubjectStudy());

    // TODO EXCEPTIONHANDLING
}