@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(); } }
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(); } } }
private Long createAndStorePerson(String firstname, String lastname) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Person thePerson = new Person(); thePerson.setFirstname(firstname); thePerson.setLastname(lastname); session.save(thePerson); session.getTransaction().commit(); return thePerson.getId(); }
private Long createAndStoreEvent(String title, Date theDate) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(theDate); session.save(theEvent); session.getTransaction().commit(); return theEvent.getId(); }
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(); }
@Override public void saveUserTopic(UserTopic userTopic) { Session session = this.sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(userTopic); tx.commit(); // System.out.println("Forum Committed..") } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } }
@Override public void savePostDetails(Post post) { // TODO Auto-generated method stub Session session = this.sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(post); tx.commit(); // System.out.println("Post Committed.."); } catch (HibernateException e) { if (tx != null) { tx.rollback(); e.printStackTrace(); } } finally { session.close(); } }