public Integer addPicture(User user, String path) throws Exception { Session session = HibernateSessionFactory.getSession(); Transaction transaction = session.beginTransaction(); try { PictureDAO dao = new PictureDAO(); TPicture picture = new TPicture(); picture.setFilenamePath(path); picture.setUser(user); picture.setMainPagePic(0); picture.setDeleted(0); picture = dao.savePicture(picture); transaction.commit(); return picture.getPictureId(); } catch (Exception e) { transaction.rollback(); throw e; } finally { HibernateSessionFactory.closeSession(); } }
public void setMainPicture(User user, int pictureId) throws Exception { Session session = HibernateSessionFactory.getSession(); Transaction transaction = session.beginTransaction(); try { // update pictures Iterator pictures = user.getPictures().iterator(); while (pictures.hasNext()) { TPicture picture = (TPicture) pictures.next(); if (picture.getPictureId() == pictureId) { picture.setMainPagePic(1); } else { picture.setMainPagePic(0); } PictureDAO pictureDao = new PictureDAO(); pictureDao.savePicture(picture); } transaction.commit(); } catch (Exception e) { transaction.rollback(); throw e; } finally { HibernateSessionFactory.closeSession(); } }