public void deleteAdmin(Long id) throws DBException { Session session; Admin admin = null; Transaction tx = null; try { session = HibernateSessionFactory.currentSession(); tx = session.beginTransaction(); admin = (Admin) session.load(Admin.class, id); session.delete(admin); tx.commit(); } catch (HibernateException e) { try { tx.rollback(); } catch (HibernateException e1) { e1.printStackTrace(); } e.printStackTrace(); throw new DBException("更新对象失败!"); } finally { try { HibernateSessionFactory.closeSession(); } catch (HibernateException e) { e.printStackTrace(); } } }
public void updatePerson(Person person) throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); session.update(person); transaction.commit(); HibernateSessionFactory.closeSession(); }
public static void main(String[] args) throws HibernateException { Person person = new Person(); person.setName("John Smith"); person.setEmail("*****@*****.**"); Session session = _sessions.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(person); tx.commit(); } catch (HibernateException he) { if (tx != null) tx.rollback(); throw he; } finally { session.close(); } }
public static void main(String[] args) throws HibernateException { Session s = HibernateUtil.currentSession(); Transaction tx = s.beginTransaction(); // Batiment Copernic = (Batiment) s.load(Batiment.class, new Long(1)); Iterator itBat = s.iterate("from Batiment"); while (itBat.hasNext()) { Batiment bat = (Batiment) itBat.next(); System.out.println(bat.getNomBat()); Iterator etageIt = bat.getEtages().iterator(); while (etageIt.hasNext()) { Etage e = (Etage) etageIt.next(); System.out.println("\t" + e.getNomEtage()); Iterator salleIt = e.getSalles().iterator(); while (salleIt.hasNext()) { Salle salle = (Salle) salleIt.next(); System.out.println( "\t\t" + salle.getNomSalle() + "\t" + salle.getTypeSalle() + "\t" + salle.getNbplaces()); } } } tx.commit(); HibernateUtil.closeSession(); }
public void updateAdmin(Admin admin) throws DBException { Session session; Transaction tx = null; try { session = HibernateSessionFactory.currentSession(); tx = session.beginTransaction(); session.update(admin); tx.commit(); } catch (HibernateException e) { try { tx.rollback(); } catch (HibernateException e1) { e1.printStackTrace(); } e.printStackTrace(); throw new DBException("获取对象失败!"); } finally { try { HibernateSessionFactory.closeSession(); } catch (HibernateException e) { e.printStackTrace(); } } }
public static ScrCity getScrCityById(String sessionID, int id, String entidad) throws AttributesException, SessionException, ValidationException { Validator.validate_String_NotNull_LengthMayorZero( sessionID, ValidationException.ATTRIBUTE_SESSION); ScrCity result = null; Transaction tran = null; try { Session session = HibernateUtil.currentSession(entidad); tran = session.beginTransaction(); // Recuperamos la sesión CacheFactory.getCacheInterface().getCacheEntry(sessionID); result = (ScrCity) session.load(ScrCity.class, new Integer(id)); HibernateUtil.commitTransaction(tran); return result; } catch (SessionException sE) { HibernateUtil.rollbackTransaction(tran); throw sE; } catch (Exception e) { HibernateUtil.rollbackTransaction(tran); log.error("Impossible to load ScrCity for session [" + sessionID + "]", e); throw new AttributesException(AttributesException.ERROR_CANNOT_FIND_CITIES); } finally { HibernateUtil.closeSession(entidad); } }
public void savePerson(Person person) throws HibernateException { person.setCreateDate(new Date()); Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); session.save(person); transaction.commit(); HibernateSessionFactory.closeSession(); }
public TemplateData loadTemplate(String id) throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); TemplateData result = (TemplateData) session.load(TemplateData.class, id); transaction.commit(); HibernateSessionFactory.closeSession(); return result; }
public Person loadPerson(String id) throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); Person newPperson = (Person) session.load(Person.class, id); transaction.commit(); HibernateSessionFactory.closeSession(); return newPperson; }
public static ValidationResults getProvForCities( String sessionID, int firstRow, int maxResults, String where, String additionalFieldName, String docColName, String entidad) throws AttributesException, SessionException, ValidationException { Validator.validate_String_NotNull_LengthMayorZero( sessionID, ValidationException.ATTRIBUTE_SESSION); Validator.validate_String_NotNull_LengthMayorZero( sessionID, ValidationException.ATTRIBUTE_SESSION); ValidationResults result = new ValidationResults(); Transaction tran = null; try { Session session = HibernateUtil.currentSession(entidad); tran = session.beginTransaction(); // Recuperamos la sesión CacheFactory.getCacheInterface().getCacheEntry(sessionID); // Calculamos el tamaño de los resultados StringBuffer querySize = new StringBuffer(); querySize.append("select count(*) from "); querySize.append(HIBERNATE_ScrProv); querySize.append(" as scr"); if (where != null && where.length() > 0) { querySize.append(" as scr where "); } Criteria criteriaResults = session.createCriteria(ScrProv.class); result = getValidationResults( session, criteriaResults, querySize.toString(), where, 1, firstRow, maxResults, "name"); result.setAdditionalFieldName(additionalFieldName); result.setDocColName(docColName); // result.setResults(provincies); HibernateUtil.commitTransaction(tran); return result; } catch (Exception e) { log.error( "Impossible to load provinces for getProvForCities for session [" + sessionID + "]", e); throw new AttributesException(AttributesException.ERROR_CANNOT_FIND_PROV); } finally { HibernateUtil.closeSession(entidad); } }
@SuppressWarnings("unchecked") public List<TemplateData> loadTemplateList() throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = session.createCriteria(TemplateData.class); List<TemplateData> list = criteria.list(); transaction.commit(); HibernateSessionFactory.closeSession(); return list; }
public ActionForward query( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String room = request.getParameter("room"); String strDate = request.getParameter("date"); Date startDay = UtilDateTime.getThisWeekDay(strDate, 1); Date endDay = UtilDateTime.getDiffDay(startDay, 6); try { Session hs = Hibernate2Session.currentSession(); Transaction tx = null; tx = hs.beginTransaction(); List valueList = null; if (room == null || room.trim().equals("")) { Query query = hs.createQuery( "from BookingRoomVO as br where br.bookingDate>=? and br.bookingDate<=? order by br.bookingDate,br.room,br.startTime"); query.setDate(0, startDay); query.setDate(1, endDay); valueList = query.list(); } else { Query query = hs.createQuery( "from BookingRoomVO as br where br.room=? and br.bookingDate>=? and br.bookingDate<=? order by br.bookingDate,br.room,br.startTime"); query.setString(0, room); query.setDate(1, startDay); query.setDate(2, endDay); valueList = query.list(); } request.setAttribute("valueList", valueList); request.setAttribute("startDay", startDay); hs.flush(); tx.commit(); } catch (Exception e) { e.printStackTrace(); // log.error(e.getMessage()); } finally { try { Hibernate2Session.closeSession(); } catch (HibernateException e1) { // log.error(e1.getMessage()); e1.printStackTrace(); } catch (SQLException e1) { // log.error(e1.getMessage()); e1.printStackTrace(); } } return mapping.findForward("query-success"); }
public TemplateData loadByResourceKey(String key) throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = session.createCriteria(TemplateData.class); criteria.add(Expression.eq("resourceKey", key)); criteria.setMaxResults(1); TemplateData result = (TemplateData) criteria.uniqueResult(); transaction.commit(); HibernateSessionFactory.closeSession(); return result; }
public TemplateData findTemplateByName(String name) throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = session.createCriteria(TemplateData.class); criteria.add(Expression.eq("name", name)); criteria.setMaxResults(1); TemplateData result = (TemplateData) criteria.uniqueResult(); transaction.commit(); HibernateSessionFactory.closeSession(); return result; }
public Person findPersonByEmail(String emailAddress) throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = session.createCriteria(Person.class); // criteria.createAlias("emailAddesses", "email"); criteria.add(Expression.eq("emailAddress", emailAddress)); // criteria.add(Expression.like("realname", personalName, // MatchMode.ANYWHERE)); criteria.setMaxResults(1); Person person = (Person) criteria.uniqueResult(); transaction.commit(); HibernateSessionFactory.closeSession(); return person; }
@SuppressWarnings("unchecked") public List<Person> loadPersonList(boolean selectable) throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = session.createCriteria(Person.class); criteria.add(Expression.eq("active", true)); if (selectable) { criteria.add(Expression.eq("selectable", true)); } List<Person> list = criteria.list(); transaction.commit(); HibernateSessionFactory.closeSession(); return list; }
public Person authenticatePerson(String username, char[] password) throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = session.createCriteria(Person.class); criteria.add(Expression.eq("username", username)); criteria.setMaxResults(1); Person person = (Person) criteria.uniqueResult(); transaction.commit(); HibernateSessionFactory.closeSession(); logger.debug("Found person: " + person.getUsername()); if (person.checkPlainPassword(new String(password))) { return person; } return null; }
private List getLogEntries(String nhsno, Calendar startdate, Calendar enddate) throws HibernateException { List logEntries = new ArrayList(); if (nhsno != null && !nhsno.equals("")) { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(LogEntry.class); criteria.add(Expression.between("date", startdate, enddate)); criteria.add(Expression.like("nhsno", "%" + nhsno + "%")); criteria.addOrder(Order.asc("id")); logEntries = criteria.list(); tx.commit(); HibernateUtil.closeSession(); } return logEntries; }
/** * Updates a class in database * * @param c IClass to update * @throws HibernateException */ public static void update(IClass c) throws HibernateException { Session s = null; Transaction tx = null; try { s = HibernateUtil.currentSession(); tx = s.beginTransaction(); s.update(c); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); if (tx != null) tx.rollback(); throw e; } finally { HibernateUtil.closeSession(); } }
/** * Returns a list of all available workflows, i.e., workflows defined in workflows.xml * * @param userPrincipal a user principal * @return a list WorkflowVOs representing available workflows */ public List<WorkflowVO> getAvailableWorkflowVOList(InfoGluePrincipal userPrincipal) throws SystemException { final List<WorkflowVO> accessibleWorkflows = new ArrayList<WorkflowVO>(); Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); WorkflowFacade wf = new WorkflowFacade(userPrincipal, hibernateSessionFactory, session); final List<WorkflowVO> allWorkflows = wf.getDeclaredWorkflows(); for (final Iterator<WorkflowVO> i = allWorkflows.iterator(); i.hasNext(); ) { final WorkflowVO workflowVO = i.next(); if (getIsAccessApproved(workflowVO.getName(), userPrincipal)) { accessibleWorkflows.add(workflowVO); } } session.flush(); tx.commit(); } catch (Exception e) { logger.error( "An error occurred when we tries to execute getAvailableWorkflowVOList():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction():" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } return accessibleWorkflows; }
/** * @param principal the user principal representing the desired user * @param name the name of the workflow to create. * @param actionId the ID of the initial action * @param inputs the inputs to the workflow * @return a WorkflowVO representing the newly created workflow instance * @throws SystemException if an error occurs while initiaizing the workflow */ public WorkflowVO initializeWorkflow( InfoGluePrincipal principal, String name, int actionId, Map inputs) throws SystemException { WorkflowVO workflowVO = null; try { Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); if (getIsAccessApproved(name, principal)) { WorkflowFacade wf = new WorkflowFacade( principal, name, actionId, inputs, hibernateSessionFactory, session); workflowVO = wf.createWorkflowVO(); session.flush(); tx.commit(); } else { throw new Bug("You are not allowed to create " + name + " workflows."); } } catch (Exception e) { logger.error( "An error occurred when we tries to run initializeWorkflow():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close:" + e.getMessage(), e); } } } catch (Exception e) { throw new SystemException(e); } return workflowVO; }
@SuppressWarnings("unchecked") public Person loadPersonForPrincipal(Principal principal) throws HibernateException { // Principal principal = request.getUserPrincipal(); Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = session.createCriteria(Person.class); criteria.add(Expression.eq("username", principal.getName())); criteria.setMaxResults(1); List<Person> personList = criteria.list(); Person person = null; if (personList.size() == 1) { person = personList.get(0); } transaction.commit(); HibernateSessionFactory.closeSession(); return person; }
public List<Person> loadExcludedPersonList(List<String> excludeList, boolean selectable) throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = session.createCriteria(Person.class); criteria.add(Expression.eq("active", true)); if (selectable) { criteria.add(Expression.eq("selectable", true)); } if (excludeList != null && !excludeList.isEmpty()) { criteria.add(Expression.not(Expression.in("id", excludeList))); } List<Person> list = criteria.list(); transaction.commit(); HibernateSessionFactory.closeSession(); return list; }
/** * Method to get all the classes * * @param parent * @return List of IClass * @throws HibernateException */ public static List<IClass> get(ITrack parent) throws HibernateException { Session s = null; Transaction tx = null; List<IClass> l = null; try { s = HibernateUtil.currentSession(); tx = s.beginTransaction(); l = s.find("FROM ClassImpl as c WHERE c.parentTrack = ?", parent.getIdData(), Hibernate.LONG); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); if (tx != null) tx.rollback(); throw e; } finally { HibernateUtil.closeSession(); } return l; }
public Map getProperties(InfoGluePrincipal userPrincipal, long workflowId) { if (logger.isDebugEnabled()) { logger.info("userPrincipal:" + userPrincipal); logger.info("workflowId:" + workflowId); } Map parameters = new HashMap(); Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); PropertySet propertySet = getPropertySet(userPrincipal, workflowId, session); for (Iterator keys = propertySet.getKeys().iterator(); keys.hasNext(); ) { String key = (String) keys.next(); parameters.put(key, propertySet.getString(key)); } session.flush(); tx.commit(); } catch (Exception e) { logger.error("An error occurred when we tries to run getHistorySteps():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } return parameters; }
/** * Returns the workflows owned by the specified principal. * * @param userPrincipal a user principal. * @return a list of WorkflowVOs owned by the principal. * @throws SystemException if an error occurs while finding the workflows */ public List getMyCurrentWorkflowVOList(InfoGluePrincipal userPrincipal) throws SystemException { List list = new ArrayList(); Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); WorkflowFacade wf = new WorkflowFacade(userPrincipal, hibernateSessionFactory, session); list = wf.getMyActiveWorkflows(userPrincipal); session.flush(); tx.commit(); } catch (Exception e) { logger.error( "An error occurred when we tries to execute getMyCurrentWorkflowVOList():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } return list; // return new WorkflowFacade(userPrincipal, true).getMyActiveWorkflows(userPrincipal); }
/** * This method will be used to establish the session with the database. Declared in AbstractDAO * class. * * @throws DAOException */ public void openSession(SessionDataBean sessionDataBean) throws DAOException { // Logger.out.info("Session opened:------------------------"); try { session = DBUtil.currentSession(); // Logger.out.info("Transaction begin:---------------------"); transaction = session.beginTransaction(); auditManager = new AuditManager(); if (sessionDataBean != null) { auditManager.setUserId(sessionDataBean.getUserId()); auditManager.setIpAddress(sessionDataBean.getIpAddress()); } else { auditManager.setUserId(null); } } catch (HibernateException dbex) { Logger.out.error(dbex.getMessage(), dbex); throw handleError(Constants.GENERIC_DATABASE_ERROR, dbex); } }
public static IClass getClassFromId(Long idClass) throws HibernateException { IClass c = null; Session s = null; Transaction tx = null; try { s = HibernateUtil.currentSession(); tx = s.beginTransaction(); c = (IClass) s.load(ClassImpl.class, idClass); tx.commit(); System.out.println(c); } catch (HibernateException e) { e.printStackTrace(); if (tx != null) tx.rollback(); throw e; } finally { HibernateUtil.closeSession(); } return c; }
/** * Returns all current steps for a workflow, i.e., steps that could be performed in the workflow's * current state * * @param userPrincipal a user principal * @param workflowId the Id of the desired workflow * @return a list of WorkflowStepVOs representing the current steps of the workflow with * workflowId */ public List getCurrentSteps(InfoGluePrincipal userPrincipal, long workflowId) { List currentSteps = new ArrayList(); Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); WorkflowFacade wf = new WorkflowFacade(userPrincipal, workflowId, hibernateSessionFactory, session); currentSteps = wf.getCurrentSteps(); session.flush(); tx.commit(); } catch (Exception e) { logger.error("An error occurred when we tries to run getCurrentSteps():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } // WorkflowFacade wf = new WorkflowFacade(userPrincipal, workflowId, true); // List currentSteps = wf.getCurrentSteps(); return currentSteps; }
private void parseMetadata() throws Exception { Session hSession = null; Transaction tx = null; try { hSession = mSessions.openSession(); tx = hSession.beginTransaction(); MSystem hSystem = doSystem(hSession); doResource(hSession, hSystem); doClasses(hSession); doObjects(hSession); doSearchHelp(hSession); doEditMask(hSession); doLookup(hSession); doLookupTypes(hSession); doUpdateHelp(hSession); doValidationExternal(hSession); doUpdate(hSession); doTable(hSession); doValidationLookup(hSession); doValidationLookupType(hSession); doValidationExternalType(hSession); doValidationExpression(hSession); doUpdateType(hSession); doForeignKey(hSession, hSystem); tx.commit(); hSession.close(); } catch (Exception e) { e.printStackTrace(); try { tx.rollback(); hSession.close(); } catch (Exception e2) { e2.printStackTrace(); } } }