public void savePartnerRequest(ActivityPartnerRequest activityPartnerRequest) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List result = session .createQuery( "from ActivityPartnerRequest as partnerrequest " + "where partnerrequest.idactivity=" + activityPartnerRequest.getIdactivity() + " and partnerrequest.iduser=" + activityPartnerRequest.getIduser()) .list(); transaction.commit(); if (result != null && result.size() > 0) { Session session1 = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction1 = session1.beginTransaction(); for (Object o : result) { session1.delete(o); } transaction1.commit(); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } saveObject(activityPartnerRequest); }
public void saveEvent(Event event) { Event event1 = getEvent(event); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { if (event1 == null) { Transaction transaction = session.beginTransaction(); try { session.save(event); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } } else { Transaction transaction = session.beginTransaction(); try { session.update(event); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } }
/** * Gets Hibernate session. * * @param tx Cache transaction. * @return Session. */ Session session(@Nullable GridCacheTx tx) { Session ses; if (tx != null) { ses = tx.meta(ATTR_SES); if (ses == null) { ses = sesFactory.openSession(); ses.beginTransaction(); // Store session in transaction metadata, so it can be accessed // for other operations on the same transaction. tx.addMeta(ATTR_SES, ses); if (log.isDebugEnabled()) log.debug("Hibernate session open [ses=" + ses + ", tx=" + tx.xid() + "]"); } } else { ses = sesFactory.openSession(); ses.beginTransaction(); } return ses; }
public void saveEventPassesRequest(EventNumberOfPassesRequest eventNumberOfPassesRequest) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List result = session .createQuery( "from EventNumberOfPassesRequest as eventNumberOfPassesRequest " + "where eventNumberOfPassesRequest.idevent=" + eventNumberOfPassesRequest.getIdevent() + " and eventNumberOfPassesRequest.iduser=" + eventNumberOfPassesRequest.getIduser()) .list(); transaction.commit(); if (result != null && result.size() > 0) { Session session1 = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction1 = session1.beginTransaction(); for (Object o : result) { session1.delete(o); } transaction1.commit(); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } saveObject(eventNumberOfPassesRequest); }
public boolean createUser(User user) { boolean result = false; if (user.getEmployeeId() != null) { User userById = getUserByEmployeeId(user.getEmployeeId()); if (userById != null) { MessageUtils.createMessage("Brugeren eksisterer allerede."); return false; } } Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); try { session.saveOrUpdate(user); result = true; session.flush(); transaction.commit(); } catch (StaleObjectStateException sose) { System.out.println("StaleObjectException"); transaction.rollback(); } catch (HibernateException e) { e.printStackTrace(); transaction.rollback(); result = false; } finally { if (session.isOpen()) { session.close(); } } return result; }
public User getUser(String username) { User result = null; Session session; Object user; session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = null; user = null; try { transaction = session.beginTransaction(); Query query = session.createQuery("from User as user where user.userName='******'"); user = query.uniqueResult(); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } if (user != null) { result = (User) user; } } finally { if (session.isOpen()) { if (session.isOpen()) { session.disconnect(); session.close(); } } } return result; }
public List<Activity> getActivities(Event parent) { List<Activity> result = new ArrayList<Activity>(); Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); Object event = session .createQuery("from Event as event where event.idevent= " + parent.getIdevent()) .uniqueResult(); transaction.commit(); if (event != null) { Event eventEntity = (Event) event; Collection<Activity> activityList = eventEntity.getActivities(); for (Activity activity : activityList) { result.add(activity); } } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } return result; }
public Location getLocation(int id) { Location result = null; Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); Object o = session .createQuery("from Location as location where location.idlocation = " + id) .uniqueResult(); transaction.commit(); if (o != null) { if (o instanceof Location) { Location location = (Location) o; result = new Location(); result.setName(location.getName()); result.setIdlocation(location.getIdlocation()); } } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } return result; }
public ClassificationValue getClassification(String value) { ClassificationValue result = null; Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); Object o = session .createQuery( "from ClassificationValue as classificationValue where classificationValue.value like '" + value + "'") .uniqueResult(); transaction.commit(); if (o != null) { if (o instanceof ClassificationValue) { result = (ClassificationValue) o; } } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } return result; }
public Location getLocation(String s) { Location result = null; Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); Object o = session .createQuery("from Location as location where location.name like '" + s + "'") .uniqueResult(); transaction.commit(); if (o != null) { if (o instanceof Location) { result = (Location) o; } } } finally { if (session.isOpen()) { session.flush(); session.disconnect(); session.close(); } } return result; }
public List<Team> getTeams() { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); List result = null; try { Transaction transaction = session.beginTransaction(); result = session.createQuery("from Team").list(); session.flush(); transaction.commit(); } catch (HibernateException e) { e.printStackTrace(); } finally { if (session.isOpen()) { session.close(); } } List<Team> teams = new ArrayList<Team>(); for (Object o : result) { if (o instanceof Team) { teams.add((Team) o); } } return teams; }
public ActivityPartnerRequest getPartnerRequest(User user, Activity activity) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List result = session .createQuery( "from ActivityPartnerRequest as partnerrequest " + "where partnerrequest.idactivity=" + activity.getIdactivity() + " and partnerrequest.iduser=" + user.getIduser()) .list(); transaction.commit(); if (result != null && result.size() > 0) { return (ActivityPartnerRequest) result.get(0); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return null; }
public List<User> getUserSearchResults(int topicId) { // TODO Auto-generated method stub List<User> userArr = new ArrayList<User>(); Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Query query1 = session.createQuery("select userId from UserTopic where topicId = :topicId"); query1.setString("topicId", String.valueOf(topicId)); Query query2 = session.createQuery("from User where userId in (:userList)"); query2.setParameterList("userList", query1.list()); userArr = query2.list(); /*for(Topic topic : topicArr){ System.out.println("Topic Description----"+topic.getTopicDescription()); System.out.println("Topic Id----"+topic.getTopicId()); }*/ } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } return userArr; }
public EventNumberOfPassesRequest getEventPassesRequest(User user, Event event) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); try { Transaction transaction = session.beginTransaction(); List result = session .createQuery( "from EventNumberOfPassesRequest as eventNumberOfPassesRequest " + "where eventNumberOfPassesRequest.idevent=" + event.getIdevent() + " and eventNumberOfPassesRequest.iduser=" + user.getIduser()) .list(); transaction.commit(); if (result != null && result.size() > 0) { return (EventNumberOfPassesRequest) result.get(0); } } finally { if (session.isOpen()) { session.disconnect(); session.close(); } } return null; }
@Override public void save(Group group) throws TransactionFailException { Session session = null; Transaction tx = null; try { session = sessionFactory.openSession(); tx = session.beginTransaction(); if (group.getId() == null) { session.persist(group); } else { session.saveOrUpdate(group); } tx.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (!tx.wasCommitted()) { tx.rollback(); session.close(); throw new TransactionFailException(); } session.close(); } }
@Override public boolean storeUser(User u) throws Exception { dLog.info("Entering method storeUser | User ID: " + u.getId()); boolean result = false; Session session = null; try { // ensure we were passed a valid object before attempting to write if (u.validate()) { session = getSession(); Transaction tranx = session.beginTransaction(); session.save(u); tranx.commit(); result = tranx.wasCommitted(); dLog.info("Was committed: " + result); } } catch (Exception e) { dLog.error("Exception in storeUser", e); } finally { // ensure that session is close regardless of the errors in try/catch if (session != null) { session.close(); } } return result; }
@Override public void delete(Group group) throws TransactionFailException { Session session = null; Transaction tx = null; try { session = sessionFactory.openSession(); tx = session.beginTransaction(); session.delete(group); tx.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (!tx.wasCommitted()) { tx.rollback(); session.close(); throw new TransactionFailException(); } session.close(); } }
@Override public List<Forum> getForumsOfaUser(int userId) { // TODO Auto-generated method stub List<Forum> forums = new ArrayList<Forum>(); Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Query query = session.createQuery("from Subscription where userId = :userId"); query.setString("userId", String.valueOf(userId)); List<Subscription> userSubscribedGroups = query.list(); for (Subscription subsc : userSubscribedGroups) { query = session.createQuery("from Forum where forumId = :forumId"); query.setParameter("forumId", subsc.getForumId()); // add this to a list variable query.uniqueResult(); forums.add((Forum) query.uniqueResult()); } } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } return forums; }
public int getCountOfSubscribers(int forumId, int userId) { // TODO Auto-generated method stub Session session = this.sessionFactory.openSession(); Transaction tx = null; int countOfSubscribers = 0; try { tx = session.beginTransaction(); Query query = null; if (userId == 0) { query = session.createQuery("select count(*) from Subscription where forumId = :forumId"); query.setParameter("forumId", forumId); } else { query = session.createQuery( "select count(*) from Subscription where forumId = :forumId and userId = :userId"); query.setParameter("forumId", forumId); query.setParameter("userId", userId); } Long count = (Long) query.uniqueResult(); countOfSubscribers = count.intValue(); // System.out.println("No of subscribers.."+countOfSubscribers); } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } return countOfSubscribers; }
@Override public void saveEventDetails(Event event) { // TODO Auto-generated method stub // Opening a session to create a database connection Session session = this.sessionFactory.openSession(); // for CRUD operations, we need to create a transaction Transaction tx = null; // adding a try catch block for maintaining the atomicity of transaction try { tx = session.beginTransaction(); System.out.println("DbHelper..saveEventDetails"); // session save inserts the object into DB session.save(event); tx.commit(); System.out.println("Event saved"); } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } }
@Override public List<Reply> getRepliesToPost(int postId) { // TODO Auto-generated method stub Session session = sessionFactory.openSession(); Transaction tx = null; List<Reply> replyArr = new ArrayList<Reply>(); try { tx = session.beginTransaction(); Query query = session.createQuery("from Reply where postId = :postId order by createdDate desc"); query.setParameter("postId", postId); replyArr = query.list(); /* for(Reply reply : replyArr){ System.out.println("Reply - id----"+reply.getReplyId()); System.out.println("Reply - Description----"+reply.getDescription()); System.out.println("Reply - Post id----"+reply.getPostId()); }*/ } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } return replyArr; }
@Override public void deleteSubscription(int userId, int forumId) { // TODO Auto-generated method stub Session session = this.sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Query query = session.createQuery( "delete from Subscription where userId = :userId and forumId = :forumId"); query.setParameter("userId", userId); query.setParameter("forumId", forumId); int result = query.executeUpdate(); System.out.println("Execute query.."); if (result > 0) { tx.commit(); // System.out.println("Subscription Removed.."); } else { // System.out.println("Subscription does not exist"); tx.rollback(); } } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } }
@Override public ArrayList<User> getAllUsers() throws Exception { dLog.info("Entering method getAllUsers"); ArrayList<User> result = new ArrayList<User>(); Session session = null; try { session = getSession(); session.clear(); Transaction tranx = session.beginTransaction(); String hql = "select from User"; Query query = session.createQuery(hql); List<?> list = query.list(); for (int n = 0; n < list.size(); n++) { result.add((User) list.get(n)); } tranx.commit(); session.flush(); session.evict(User.class); } catch (Exception e) { dLog.error("Exception in getAllUsers", e); } finally { // ensure that session is close regardless of the errors in try/catch if (session != null) { session.close(); } } return result; }
public void save(Treatment treatment) { Session session = this.sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.persist(treatment); tx.commit(); session.close(); }
public void runHibernate() { // ############################################################################ Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); Message message = new Message("Hello World"); session.save(message); tx.commit(); session.close(); // ############################################################################ Session secondSession = HibernateUtil.getSessionFactory().openSession(); Transaction secondTransaction = secondSession.beginTransaction(); List messages = secondSession.createQuery("from Message m order by m.text asc").list(); System.out.println(messages.size() + " message(s) found:"); for (Iterator iter = messages.iterator(); iter.hasNext(); ) { Message loadedMsg = (Message) iter.next(); System.out.println(loadedMsg.getText()); } secondTransaction.commit(); secondSession.close(); // ############################################################################ Session thirdSession = HibernateUtil.getSessionFactory().openSession(); Transaction thirdTransaction = thirdSession.beginTransaction(); // message.getId() holds the identifier value of the first message Message loadedMessage = (Message) thirdSession.get(Message.class, message.getId()); loadedMessage.setText("Greetings Earthling"); loadedMessage.setNextMessage(new Message("Take me to your leader (please)")); thirdTransaction.commit(); thirdSession.close(); // ############################################################################ // Final unit of work (just repeat the query) // TODO: You can move this query into the thirdSession before the // commit, makes more sense! Session fourthSession = HibernateUtil.getSessionFactory().openSession(); Transaction fourthTransaction = fourthSession.beginTransaction(); messages = fourthSession.createQuery("from Message m order by m.text asc").list(); System.out.println(messages.size() + " message(s) found:"); for (Iterator iter = messages.iterator(); iter.hasNext(); ) { Message loadedMsg = (Message) iter.next(); System.out.println(loadedMsg.getText()); } fourthTransaction.commit(); fourthSession.close(); // Shutting down the application HibernateUtil.shutdown(); }
public Team getTeam(String name) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); Team team = (Team) session.createQuery("from Team as team where team.name= '" + name + "'").uniqueResult(); transaction.commit(); return team; }
public User getUser(int iduser) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); User user = (User) session.createQuery("from User as user where user.iduser = " + iduser).uniqueResult(); transaction.commit(); return user; }
public Team getTeam(Team teamSelection) { Session session = SessionFactoryUtil.getInstance().getCurrentSession(); Transaction transaction = session.beginTransaction(); Team team = (Team) session .createQuery("from Team as team where team.id = " + teamSelection.getId()) .uniqueResult(); transaction.commit(); return team; }
private List listEvents() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List result = session.createQuery("from Event").list(); session.getTransaction().commit(); return result; }
private void addEmailToPerson(Long personId, String emailAddress) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Person aPerson = (Person) session.load(Person.class, personId); // The getEmailAddresses() might trigger a lazy load of the collection aPerson.getEmailAddresses().add(emailAddress); session.getTransaction().commit(); }