Example usage for org.hibernate Session saveOrUpdate

List of usage examples for org.hibernate Session saveOrUpdate

Introduction

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

Prototype

void saveOrUpdate(Object object);

Source Link

Document

Either #save(Object) or #update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

Usage

From source file:br.univel.common.Dao.java

public void update(Model model) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/* w w  w. j a va 2s .  c o m*/
    session.saveOrUpdate(model);
    session.getTransaction().commit();
    session.close();
    //    HibernateUtil.finalizar();
}

From source file:by.andrew.pakhomov.testtask.web.controller.ProjectController.java

@RequestMapping(path = "/{id}/tasks/new", method = RequestMethod.POST)
public String addTaskToProject(@RequestParam String title, @RequestParam String responsible,
        @PathVariable("id") int projectId, RedirectAttributes redirectAttrs) {
    final String taskTitle = title.trim();
    final String taskResponsible = responsible.trim();
    List<String> errors = new ArrayList<>(2);
    if (title.isEmpty()) {
        errors.add("  \"? \"");
    }/*w w  w.  j a  va  2  s .  co m*/
    if (responsible.isEmpty()) {
        errors.add("  \"?\"");
    }
    if (!errors.isEmpty()) {
        this.addFormErrors(redirectAttrs, errors.toArray(new String[errors.size()]));
        redirectAttrs.addFlashAttribute("title", title);
        redirectAttrs.addFlashAttribute("responsible", responsible);
        return "redirect:/projects/" + projectId;
    }

    boolean success = this.executeSafely(redirectAttrs, (Session session) -> {
        Project project = (Project) session.get(Project.class, projectId);
        if (project == null) {
            throw new ResourceNotFoundException();
        }
        Task task = project.addTask(taskTitle, taskResponsible);
        session.saveOrUpdate(project);
        this.addInfoMessage(redirectAttrs,
                " " + task.getTitle() + "\" ? ?");
        return true;
    });

    if (!success) {
        redirectAttrs.addFlashAttribute("title", title);
        redirectAttrs.addFlashAttribute("responsible", responsible);
    }

    return "redirect:/projects/" + projectId;
}

From source file:by.andrew.pakhomov.testtask.web.controller.TaskController.java

@RequestMapping(path = "/{id}/add", method = RequestMethod.POST)
public String addTimeSpentRecord(@PathVariable("id") int taskId, @RequestParam("spentTime") String spentTime,
        @RequestParam("employee") String employee, @RequestParam("registrationDate") String registrationDate,
        RedirectAttributes redirectAttrs) {
    List<String> errors = new ArrayList<>(3);
    Date registrationDateTime = null;

    String[] timeParts = spentTime.split(":");
    int hh = 0;//from  w  ww  . j  a  va2 s .  c  o  m
    int mm = 0;
    if (timeParts.length == 2) {
        try {
            hh = Integer.valueOf(timeParts[0]);
            mm = Integer.valueOf(timeParts[1]);
            if (mm > 59) {
                errors.add("  ?   :");
            }
        } catch (NumberFormatException ex) {
            errors.add("  ?   :");
        }
    } else {
        errors.add("  ?   :");
    }

    try {
        registrationDateTime = DATE_FORMAT.parse(registrationDate);
    } catch (ParseException ex) {
        errors.add(" ? ?   --");
    }
    if (employee.isEmpty()) {
        errors.add("  \"\"");
    }
    if (!errors.isEmpty()) {
        this.addFormErrors(redirectAttrs, errors.toArray(new String[errors.size()]));
        redirectAttrs.addFlashAttribute("registrationDate", registrationDate);
        redirectAttrs.addFlashAttribute("employee", employee);
        redirectAttrs.addFlashAttribute("spentTime", spentTime);
        return "redirect:/tasks/" + taskId;
    }

    final String spentTimeEmployee = employee.trim();
    final Long finalSpentTime = hh * 3600L + mm * 60;
    final Date finalRegistrationDate = registrationDateTime;

    boolean success = this.executeSafely(redirectAttrs, (Session session) -> {
        Task task = (Task) session.get(Task.class, taskId);
        if (task == null) {
            throw new ResourceNotFoundException();
        }
        SpentTimeRecord record = task.addSpentTimeRecord(finalRegistrationDate, finalSpentTime,
                spentTimeEmployee);
        session.saveOrUpdate(task);
        this.addInfoMessage(redirectAttrs,
                "?   ? ? "
                        + record.getEmployee() + "   "
                        + DATE_FORMAT.format(record.getRegistrationDate())
                        + " ?");
        return true;
    });

    if (!success) {
        redirectAttrs.addFlashAttribute("registrationDate", registrationDate);
        redirectAttrs.addFlashAttribute("employee", employee);
        redirectAttrs.addFlashAttribute("spentTime", spentTime);
    }

    return "redirect:/tasks/" + taskId;
}

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Try to delete a participant.//from   w  w  w.j ava  2  s .  c om
 * 
 * @param participant
 */
public void deleteParticipant(Participant participant) {
    Object project = null;
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(participant.getProject().getFolderName());
    Session session = null;
    Transaction t = null;

    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */
        project = session.get(Project.class, participant.getProject().getPersistenceId());
        Object part = session.get(Participant.class, participant.getPersistenceId());

        ((Project) project).getParticipants().remove(part);

        session.delete(part);
        session.saveOrUpdate(project);
        session.flush();
        t.commit();

        fListenerManager.notifyParticipantListeners(ChangeType.DELETE, new Participant[] { participant }, this);
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String key = "model.Facade.Participant.cannotDelete"; //$NON-NLS-1$
        String errorMessage = Messages.getString(key);
        fLogger.error(key, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Try to delete the investigator.//from  w ww.  j  av a2 s  .  c om
 * 
 * @param investigator
 */
public void deleteInvestigator(Investigator investigator) {
    Object project = null;
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(investigator.getProject().getFolderName());
    Session session = null;
    Transaction t = null;
    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */
        project = session.get(Project.class, investigator.getProject().getPersistenceId());
        Object inv = session.get(Investigator.class, investigator.getPersistenceId());

        ((Project) project).getInvestigators().remove(inv);

        session.delete(inv);
        session.saveOrUpdate(project);
        session.flush();
        t.commit();
        fListenerManager.notifyInvestigatorListeners(ChangeType.DELETE, new Investigator[] { investigator },
                this);
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String key = "model.Facade.Investigator.cannotDelete"; //$NON-NLS-1$
        String errorMessage = Messages.getString(key);
        fLogger.error(key, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Try to delete the transcript.//w ww.j a v  a 2 s  .c  o m
 * 
 * @param transcript
 */
public void deleteTranscript(Transcript transcript) {
    Object project = null;
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(transcript.getProject().getFolderName());
    Session session = null;
    Transaction t = null;
    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */
        project = session.get(Project.class, transcript.getProject().getPersistenceId());
        Transcript trans = (Transcript) session.get(Transcript.class, transcript.getPersistenceId());

        ((Project) project).getTranscripts().remove(trans);

        session.delete(trans);
        session.saveOrUpdate(project);
        session.flush();
        t.commit();
        fListenerManager.notifyTranscriptListeners(ChangeType.DELETE, new Transcript[] { transcript }, this);
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String key = "model.Facade.Transcript.cannotDelete"; //$NON-NLS-1$
        String errorMessage = Messages.getString(key);
        fLogger.error(key, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Try to delete the memo.//from  w w w  .  j  ava  2  s.c o  m
 * 
 * @param memo
 */
public void deleteMemo(Memo memo) {
    Object project = null;
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(memo.getProject().getFolderName());
    Session session = null;
    Transaction t = null;
    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */
        project = session.get(Project.class, memo.getProject().getPersistenceId());
        Memo lMemo = (Memo) session.get(Memo.class, memo.getPersistenceId());

        ((Project) project).getMemos().remove(lMemo);

        session.delete(lMemo);
        session.saveOrUpdate(project);
        session.flush();
        t.commit();
        fListenerManager.notifyMemoListeners(ChangeType.DELETE, new Memo[] { memo }, this);
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String key = "model.Facade.Memo.cannotDelete"; //$NON-NLS-1$
        String errorMessage = Messages.getString(key);
        fLogger.error(key, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Delete a code./*from  w  w w  .ja  v a 2s  . c o  m*/
 * @param code
 */
public void deleteCode(Code code) {
    Object project = null;
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(code.getProject().getFolderName());
    Session session = null;
    Transaction t = null;

    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */
        project = session.get(Project.class, code.getProject().getPersistenceId());
        Object lCode = session.get(Code.class, code.getPersistenceId());

        ((Project) project).getCodes().remove(lCode);

        session.delete(lCode);
        session.saveOrUpdate(project);
        session.flush();
        t.commit();

        fListenerManager.notifyCodeListeners(ChangeType.DELETE, new Code[] { (Code) lCode }, this);
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String errorMessage = Messages.getString("model.Facade.code.cannotDelete"); //$NON-NLS-1$
        fLogger.error(errorMessage, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }

}

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Delete a fragment./*from w  w  w .  ja  v a  2 s. c o m*/
 * @param fragment
 */
public void deleteFragment(Fragment fragment) {
    IAnnotatedDocument document = fragment.getDocument();
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(document.getProject().getFolderName());
    Session session = null;
    Transaction t = null;

    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */

        document.getFragments().remove(fragment.getOffset());
        session.delete(fragment);
        session.saveOrUpdate(document);
        session.flush();
        t.commit();

        if (document instanceof Transcript) {

            fListenerManager.notifyTranscriptListeners(ChangeType.MODIFY,
                    new Transcript[] { (Transcript) document }, this);
        } else {
            fListenerManager.notifyMemoListeners(ChangeType.MODIFY, new Memo[] { (Memo) document }, this);
        }
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String errorMessage = Messages.getString("model.Facade.fragment.cannotDelete"); //$NON-NLS-1$
        fLogger.error(errorMessage, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Delete a timestamp./*from   w ww  . ja v a 2s . c o  m*/
 * @param timestamp
 */
public void deleteTimestamp(Timestamp timestamp) {
    Transcript transcript = timestamp.getTranscript();
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(transcript.getProject().getFolderName());
    Session session = null;
    Transaction t = null;

    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */
        transcript.getTimestamps().remove(timestamp.getLineNumber());
        session.delete(timestamp);
        session.saveOrUpdate(transcript);
        session.flush();
        t.commit();

        fListenerManager.notifyTranscriptListeners(ChangeType.MODIFY, new Transcript[] { transcript }, this);
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String errorMessage = Messages.getString("model.Facade.Timestamp.cannotDelete"); //$NON-NLS-1$
        fLogger.error(errorMessage, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }
}