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
@@ -124,16 +124,55 @@
 odel.*;%0A
+import javax.annotation.PostConstruct;%0A
 import o
@@ -240,16 +240,147 @@
 Factory;
+%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.jdbc.datasource.DriverManagerDataSource;
 %0A%0Apublic
@@ -453,14 +453,8 @@
 ate 
-final 
 Sess
@@ -486,115 +486,381 @@
 ;%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(
+@Autowired%0A%09private DriverManagerDataSource ds;%0A%0A%09@PostConstruct%0A%09private void initialize() %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.setProperty(%22hibernate.connection.url%22, ds.getUrl())%0A%09%09%09%09%09.setProperty(%22hibernate.connection.username%22, ds.getUsername())%0A%09%09%09%09%09.setProperty(%22hibernate.connection.password%22, ds.getPassword()
 )%0A%09%09

Update CollectionDAO.java