List of usage examples for org.hibernate Session get
Object get(String entityName, Serializable id);
From source file:br.unisal.dao.GenericDao.java
public <T> T getById(Class<T> type, Long id) { Session session = HibernateUtil.getInstance().getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); T entity = null;//from w w w .ja v a2s. c o m try { tx.begin(); entity = (T) session.get(type, id); tx.commit(); } catch (HibernateException e) { tx.rollback(); throw new RuntimeException(e); } finally { session.close(); } return entity; }
From source file:by.andrew.pakhomov.testtask.web.controller.ProjectController.java
@RequestMapping(path = "/{id}", method = RequestMethod.GET) public String getProject(Model model, @PathVariable(value = "id") int id) { this.executeSafely(model, (Session session) -> { Project project = (Project) session.get(Project.class, id); model.addAttribute("project", project); return true; });/*from w ww . j a va 2s . com*/ return "project"; }
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(" \"? \""); }//from w w w.j a va2s. c om 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("/{id}") public String getTask(@PathVariable("id") int id, Model model) { this.executeSafely(model, (Session session) -> { Task task = (Task) session.get(Task.class, id); if (task == null) { throw new ResourceNotFoundException(); }//from w w w .j ava 2 s . c o m model.addAttribute("task", task); return true; }); return "task"; }
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 av a2 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:by.bsuir.akkerman.mobile.server.dao.DaoUniversal.java
public boolean add(EntityHelper obj) { boolean isSuccessfully = false; SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction();//from ww w . j a va2 s. c o m try { session.save(obj); if (obj.getParent() != null) { Set<EntityHelper> set = new HashSet(obj.getParent()); for (Iterator it = set.iterator(); it.hasNext();) { EntityHelper parent_obj = (EntityHelper) session.get(it.next().getClass(), ((EntityHelper) it.next()).getId()); parent_obj.getChild().add(obj); session.update(parent_obj); } } isSuccessfully = true; } catch (HibernateException ex) { System.out.println(ex.getMessage()); } finally { session.getTransaction().commit(); } return isSuccessfully; }
From source file:by.bsuir.akkerman.mobile.server.dao.DaoUniversal.java
public boolean delete(EntityHelper obj) { boolean isSuccessfully = false; SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction();//w w w .jav a2 s . com try { obj = (EntityHelper) session.get(obj.getClass(), obj.getId()); session.delete(obj); if (!obj.getParent().isEmpty()) { Set<EntityHelper> set = new HashSet(obj.getParent()); for (Iterator it = set.iterator(); it.hasNext();) { EntityHelper parent_obj = (EntityHelper) session.get(it.next().getClass(), ((EntityHelper) it.next()).getId()); parent_obj.getChild().add(obj); session.update(parent_obj); } } if (obj.getParent() != null) { Set<EntityHelper> set = new HashSet(obj.getParent()); for (Iterator it = set.iterator(); it.hasNext();) { String[] lines = it.next().getClass().getName().split("_"); EntityHelper parent_obj = (EntityHelper) session.get(lines[0], ((EntityHelper) it.next()).getId()); parent_obj.getChild().remove(obj); session.update(parent_obj); } } isSuccessfully = true; } catch (HibernateException ex) { System.out.println(ex.getMessage()); } finally { session.getTransaction().commit(); session.close(); } return isSuccessfully; }
From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java
License:Open Source License
/** * Try to delete a participant./*from w ww.j ava 2s. co m*/ * * @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./*ww w . j a v a 2 s. c o m*/ * * @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.//from www . j a v a2s.c om * * @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); } }