package com.blstream.myhoard.db.dao;

import com.blstream.myhoard.biz.exception.MyHoardException;
import java.util.List;
import org.hibernate.Session;

import com.blstream.myhoard.db.model.*;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;

public class CollectionDAO implements ResourceDAO<CollectionDS> {

	private final SessionFactory sessionFactory;

	public CollectionDAO() {
		try {
			sessionFactory = new org.hibernate.cfg.Configuration()
					.configure()
					.buildSessionFactory();
		} catch (HibernateException ex) {
			// Log the exception.
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	@Override
	public List<CollectionDS> getList() {
//		Session session = HibernateUtil.beginTransaction();
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();

		List<CollectionDS> result = session.createQuery("from CollectionDS").list();

//		HibernateUtil.commitTransaction();
		sessionFactory.getCurrentSession().getTransaction().commit();
		return result;
	}

	@Override
	public CollectionDS get(int id) throws IndexOutOfBoundsException {
//		Session session = HibernateUtil.beginTransaction();
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		List<CollectionDS> result = session.createQuery("from CollectionDS where id = " + id).list();
//		HibernateUtil.commitTransaction();
		sessionFactory.getCurrentSession().getTransaction().commit();
		return result.get(0);
	}

	@Override
	public void create(CollectionDS obj) {
//		Session session = HibernateUtil.beginTransaction();
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(obj);
//		HibernateUtil.commitTransaction();
		sessionFactory.getCurrentSession().getTransaction().commit();
	}

	@Override
	public void update(CollectionDS obj) {
		CollectionDS object = get(obj.getId());
		object.updateObject(obj);

//		Session session = HibernateUtil.beginTransaction();
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		object.setModifiedDate(java.util.Calendar.getInstance().getTime());
		session.update(object);
//		HibernateUtil.commitTransaction();
		sessionFactory.getCurrentSession().getTransaction().commit();

		obj.updateObject(object);
		obj.setOwner(object.getOwner());
		obj.setCreatedDate(object.getCreatedDate());
		obj.setModifiedDate(object.getModifiedDate());
	}

	@Override
	public void remove(int id) {
//		Session session = HibernateUtil.beginTransaction();
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.createQuery("delete CollectionDS where id = " + id).executeUpdate();
//		HibernateUtil.commitTransaction();
		sessionFactory.getCurrentSession().getTransaction().commit();
	}

}

@@ -35,68 +35,8 @@
 o;%0A%0A
-import com.blstream.myhoard.biz.exception.MyHoardException;%0A
 impo
@@ -84,17 +84,16 @@
 ession;%0A
-%0A
 import c
@@ -135,40 +135,26 @@
 ort 
-org.hibernate.HibernateException
+java.util.Calendar
 ;%0Aim
@@ -269,14 +269,8 @@
 ate 
-final 
 Sess
@@ -302,437 +302,56 @@
 ;%0A%0A%09
-public CollectionDAO() %7B%0A%09%09try %7B%0A%09%09%09sessionFactory = new org.hibernate.cfg.Configuration()%0A%09%09%09%09%09.configure()%0A%09%09%09%09%09.buildSessionFactory();%0A%09%09%7D catch (HibernateException ex) %7B%0A%09%09%09// Log the exception.%0A%09%09%09System.err.println(%22Initial SessionFactory creation failed.%22 + ex);%0A%09%09%09throw new ExceptionInInitializerError(ex);%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09public List%3CCollectionDS%3E getList() %7B%0A//%09%09Session session = HibernateUtil.beginTransaction();
+@Override%0A%09public List%3CCollectionDS%3E getList() %7B
 %0A%09%09S
@@ -433,17 +433,16 @@
 tion();%0A
-%0A
 %09%09List%3CC
@@ -516,84 +516,17 @@
 ();%0A
-%0A//%09%09HibernateUtil.commitTransaction();%0A%09%09sessionFactory.getCurrentSession()
+%09%09session
 .get
@@ -653,64 +653,8 @@
 n %7B%0A
-//%09%09Session session = HibernateUtil.beginTransaction();%0A
 %09%09Se
@@ -729,37 +729,32 @@
 ransaction();%0A%09%09
-List%3C
 CollectionDS%3E re
@@ -741,33 +741,32 @@
 ;%0A%09%09CollectionDS
-%3E
  result = sessio
@@ -751,32 +751,46 @@
 tionDS result = 
+(CollectionDS)
 session.createQu
@@ -835,91 +835,33 @@
 id).
-list();%0A//%09%09HibernateUtil.commitTransaction();%0A%09%09sessionFactory.getCurrentSession()
+uniqueResult();%0A%09%09session
 .get
@@ -903,15 +903,8 @@
 sult
-.get(0)
 ;%0A%09%7D
@@ -960,64 +960,8 @@
 ) %7B%0A
-//%09%09Session session = HibernateUtil.beginTransaction();%0A
 %09%09Se
@@ -1067,83 +1067,17 @@
 j);%0A
-//%09%09HibernateUtil.commitTransaction();%0A%09%09sessionFactory.getCurrentSession()
+%09%09session
 .get
@@ -1230,64 +1230,8 @@
 );%0A%0A
-//%09%09Session session = HibernateUtil.beginTransaction();%0A
 %09%09Se
@@ -1337,26 +1337,16 @@
 iedDate(
-java.util.
 Calendar
@@ -1402,83 +1402,17 @@
 t);%0A
-//%09%09HibernateUtil.commitTransaction();%0A%09%09sessionFactory.getCurrentSession()
+%09%09session
 .get
@@ -1644,64 +1644,8 @@
 ) %7B%0A
-//%09%09Session session = HibernateUtil.beginTransaction();%0A
 %09%09Se
@@ -1809,109 +1809,162 @@
 ();%0A
-//%09%09HibernateUtil.commitTransaction();%0A%09%09sessionFactory.getCurrentSession().getTransaction().commit()
+%09%09session.getTransaction().commit();%0A%09%7D%0A%0A%09@Override%0A%09public void setSessionFactory(SessionFactory sessionFactory) %7B%0A%09%09this.sessionFactory = sessionFactory
 ;%0A%09%7D

Update CollectionDAO.java