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 void updatePerson(Person person) throws HibernateException { Session session = HibernateSessionFactory.currentSession(); Transaction transaction = session.beginTransaction(); session.update(person); transaction.commit(); HibernateSessionFactory.closeSession(); }
/** * 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(); } }
public ActionForward perform( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // Extract attributes we will need ActionErrors errors = this.getActionErrors(request.getSession()); Logger log = Logger.getLogger(EditCustPartyAction.class.getName()); Locale locale = getLocale(request); MessageResources messages = getResources(); String action = request.getParameter("action"); String openType = request.getParameter("openType"); log.info("action=" + action); try { String PartyId = request.getParameter("PartyId"); String description = request.getParameter("description"); // String ParentpartyId = request.getParameter("ParentpartyId"); String address = request.getParameter("address"); String postcode = request.getParameter("postcode"); String telecode = request.getParameter("telecode"); String note = request.getParameter("note"); String role = request.getParameter("role"); String ChineseName = request.getParameter("ChineseName"); String AccountId = request.getParameter("AccountId"); String IndustryId = request.getParameter("IndustryId"); String t2code = request.getParameter("t2code"); String AccountCode = request.getParameter("AccountCode"); String CustPartyTypeId = "PARTY_GROUP"; if (address == null) address = ""; if (postcode == null) postcode = ""; if (telecode == null) telecode = ""; if (note == null) note = ""; if (role == null) role = ""; if (ChineseName == null) ChineseName = ""; if (AccountId == null) AccountId = "0"; if (IndustryId == null) IndustryId = "0"; if (t2code == null) t2code = ""; if (AccountCode == null) AccountCode = ""; net.sf.hibernate.Session hs = Hibernate2Session.currentSession(); Transaction tx = null; if (action == null) action = "view"; if (!isTokenValid(request)) { if (action.equals("create") || action.equals("update")) { action = "view"; } } saveToken(request); if (action.equals("view")) { log.info(action); CustomerProfile CustParty = null; if (!((PartyId == null) || (PartyId.length() < 1))) CustParty = (CustomerProfile) hs.load(CustomerProfile.class, PartyId); request.setAttribute("CustParty", CustParty); if ("dialogView".equals(openType)) { return (mapping.findForward("dialogView")); } else { return (mapping.findForward("view")); } } if (action.equals("create")) { if ((PartyId == null) || (PartyId.length() < 1)) { CustomerService cs = new CustomerService(); PartyId = cs.getCustomerNo(hs); } if ((description == null) || (description.length() < 1)) actionDebug.addGlobalError(errors, "error.context.required"); if (!errors.empty()) { saveErrors(request, errors); return (new ActionForward(mapping.getInput())); } try { tx = hs.beginTransaction(); CustomerProfile CustParty = new CustomerProfile(); CustParty.setPartyId(PartyId); CustParty.setDescription(description); CustParty.setAddress(address); CustParty.setPostCode(postcode); CustParty.setTeleCode(telecode); CustParty.setNote(note); CustParty.setType("C"); CustParty.setChineseName(ChineseName); CustParty.setAccountCode(AccountCode); Industry ind = (Industry) hs.load(Industry.class, new Long(IndustryId)); CustParty.setIndustry(ind); CustomerAccount acc = (CustomerAccount) hs.load(CustomerAccount.class, new Long(AccountId)); CustParty.setAccount(acc); CustT2Code T2C = (CustT2Code) hs.load(CustT2Code.class, t2code); CustParty.setT2Code(T2C); PartyType CustPartyType = (PartyType) hs.load(PartyType.class, CustPartyTypeId); CustParty.setPartyType(CustPartyType); Set mset = CustParty.getPartyRoles(); if (mset == null) { mset = new HashSet(); } mset.add((RoleType) hs.load(RoleType.class, role)); CustParty.setPartyRoles(mset); hs.save(CustParty); hs.flush(); tx.commit(); // edit prospect Company String prospectCompanyId = request.getParameter("prospectCompanyId"); if (prospectCompanyId != null && !prospectCompanyId.equals("") && !prospectCompanyId.equals("null")) { tx = hs.beginTransaction(); com.aof.component.prm.bid.ProspectCompany prospect = (com.aof.component.prm.bid.ProspectCompany) hs.load( com.aof.component.prm.bid.ProspectCompany.class, new Long(prospectCompanyId)); prospect.setName(description); prospect.setAddress(address); prospect.setPostCode(postcode); prospect.setTeleNo(telecode); prospect.setChineseName(ChineseName); prospect.setIndustry(IndustryId); prospect.setCustomerGroup(AccountId); prospect.setStatus("Existing"); hs.update(prospect); hs.flush(); tx.commit(); } request.setAttribute("CustParty", CustParty); log.info("go to >>>>>>>>>>>>>>>>. view forward"); } catch (Exception e) { e.printStackTrace(); } if ("dialogView".equals(openType)) { return (mapping.findForward("dialogView")); } else { return (mapping.findForward("view")); } } if (action.equals("update")) { if ((PartyId == null) || (PartyId.length() < 1)) actionDebug.addGlobalError(errors, "error.context.required"); if (!errors.empty()) { saveErrors(request, errors); return (new ActionForward(mapping.getInput())); } tx = hs.beginTransaction(); CustomerProfile CustParty = (CustomerProfile) hs.load(CustomerProfile.class, PartyId); CustParty.setDescription(description); CustParty.setAddress(address); CustParty.setPostCode(postcode); CustParty.setTeleCode(telecode); CustParty.setNote(note); CustParty.setChineseName(ChineseName); Industry ind = (Industry) hs.load(Industry.class, new Long(IndustryId)); CustParty.setIndustry(ind); CustomerAccount acc = (CustomerAccount) hs.load(CustomerAccount.class, new Long(AccountId)); CustParty.setAccount(acc); CustT2Code T2C = (CustT2Code) hs.load(CustT2Code.class, t2code); CustParty.setT2Code(T2C); CustParty.setAccountCode(AccountCode); hs.update(CustParty); tx.commit(); request.setAttribute("CustParty", CustParty); if ("dialogView".equals(openType)) { return (mapping.findForward("dialogView")); } else { return (mapping.findForward("view")); } } if (action.equals("delete")) { if ((PartyId == null) || (PartyId.length() < 1)) actionDebug.addGlobalError(errors, "error.context.required"); if (!errors.empty()) { saveErrors(request, errors); return (new ActionForward(mapping.getInput())); } tx = hs.beginTransaction(); CustomerProfile CustParty = (CustomerProfile) hs.load(CustomerProfile.class, PartyId); log.info("PartyId=" + CustParty.getPartyId()); hs.delete(CustParty); tx.commit(); return (mapping.findForward("after_delete")); } if (!errors.empty()) { saveErrors(request, errors); return (new ActionForward(mapping.getInput())); } if ("dialogView".equals(openType)) { return (mapping.findForward("dialogView")); } else { return (mapping.findForward("view")); } } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); // cannot delete due to the database constraints if (action.equals("delete")) { request.setAttribute("errorSign", "Cannot delete the record !"); } if ("dialogView".equals(openType)) { return (mapping.findForward("dialogView")); } else { return (mapping.findForward("view")); } } finally { try { Hibernate2Session.closeSession(); } catch (HibernateException e1) { log.error(e1.getMessage()); e1.printStackTrace(); } catch (SQLException e1) { log.error(e1.getMessage()); e1.printStackTrace(); } } }
public ActionForward list4Add( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Logger log = Logger.getLogger(EditPreSaleProjectAction.class.getName()); String action = request.getParameter("formAction"); // log.info("action=" + action); String strDate = request.getParameter("date"); Date date = UtilDateTime.toDate2(strDate + " 00:00:00.000"); String room = request.getParameter("room"); if (action == null) { action = "view"; } try { Session hs = Hibernate2Session.currentSession(); Transaction tx = null; if (action.equals("create")) { String personId = request.getParameter("iPerson"); String startTime = request.getParameter("iStartTime"); String endTime = request.getParameter("iEndTime"); BookingRoomVO tmpValue = new BookingRoomVO(); UserLogin person = new UserLogin(); tmpValue.setBookingDate(date); tmpValue.setRoom(room); tmpValue.setStartTime(startTime); tmpValue.setEndTime(endTime); tx = hs.beginTransaction(); person = (UserLogin) hs.load(person.getClass(), personId); tmpValue.setPerson(person); hs.save(tmpValue); hs.flush(); tx.commit(); } if (action.equals("remove")) { Long id = Long.valueOf(request.getParameter("bookingId")); tx = hs.beginTransaction(); BookingRoomVO tmpValue = (BookingRoomVO) hs.load(BookingRoomVO.class, id); hs.delete(tmpValue); hs.flush(); tx.commit(); } if (action.equals("update")) { String bookingId[] = request.getParameterValues("bookingId"); String startTime[] = request.getParameterValues("startTime"); String endTime[] = request.getParameterValues("endTime"); BookingRoomVO tmpValue = new BookingRoomVO(); tx = hs.beginTransaction(); int rowSize = java.lang.reflect.Array.getLength(bookingId); for (int i = 0; i < rowSize; i++) { Long id = null; if (bookingId[i] != null && bookingId[i].length() > 0) { id = Long.valueOf(bookingId[i]); tmpValue = (BookingRoomVO) hs.load(BookingRoomVO.class, id); tmpValue.setStartTime(startTime[i]); tmpValue.setEndTime(endTime[i]); hs.update(tmpValue); } } tx.commit(); hs.flush(); } if (action.equals("view") || action.equals("create") || action.equals("remove") || action.equals("update")) { request.setAttribute("dateAdd", strDate); request.setAttribute("roomAdd", room); tx = hs.beginTransaction(); List valueList = null; Query query = hs.createQuery( "from BookingRoomVO as br where br.room=? and br.bookingDate=? order by br.startTime"); query.setString(0, room); query.setDate(1, date); valueList = query.list(); request.setAttribute("valueList", valueList); hs.flush(); tx.commit(); } } catch (Exception e) { e.printStackTrace(); // log.error(e.getMessage()); return (mapping.findForward("view")); } 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("list4Add-success"); }
/** * Updates the persistent object in the database. * * @param obj The object to be updated. * @param session The session in which the object is saved. * @throws DAOException * @throws HibernateException Exception thrown during hibernate operations. */ public void update( Object obj, SessionDataBean sessionDataBean, boolean isAuditable, boolean isSecureUpdate, boolean hasObjectLevelPrivilege) throws DAOException, UserNotAuthorizedException { boolean isAuthorized = true; try { if (isSecureUpdate) { if (null != sessionDataBean) { if (!(obj instanceof AbstractDomainObject) || !hasObjectLevelPrivilege) { isAuthorized = SecurityManager.getInstance(this.getClass()) .isAuthorized( sessionDataBean.getUserName(), obj.getClass().getName(), Permissions.UPDATE); Logger.out.debug( " User's Authorization to update " + obj.getClass().getName() + " " + isAuthorized); } else { isAuthorized = SecurityManager.getInstance(this.getClass()) .isAuthorized( sessionDataBean.getUserName(), obj.getClass().getName() + "_" + ((AbstractDomainObject) obj).getId(), Permissions.UPDATE); Logger.out.debug( " User's Authorization to update " + obj.getClass().getName() + " " + isAuthorized); } } else { isAuthorized = false; Logger.out.debug( " User's Authorization to update " + obj.getClass().getName() + "_" + ((AbstractDomainObject) obj).getId() + " " + isAuthorized); } } if (isAuthorized) { session.update(obj); // Object oldObj = retrieve(obj.getClass().getName(), // ((Auditable)obj).getId()); // if (obj instanceof Auditable && isAuditable) // auditManager.compare((Auditable) obj, (Auditable)oldObj, "UPDATE"); isUpdated = true; } else { throw new UserNotAuthorizedException("Not Authorized to update"); } } catch (HibernateException hibExp) { // Logger.out.error(hibExp.getMessage(), hibExp); // throw new DAOException("Error in update", hibExp); throw handleError("", hibExp); } // catch (AuditException hibExp) // { // throw handleError("", hibExp); // } catch (SMException smex) { // Logger.out.error(smex.getMessage(), smex); // throw new DAOException("Error in update", smex); throw handleError("", smex); } }