package com.blstream.myhoard.db.dao;

import java.util.List;
import org.hibernate.Session;
import com.blstream.myhoard.db.model.*;
import javax.annotation.PostConstruct;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class CollectionDAO implements ResourceDAO<CollectionDS> {

	private SessionFactory sessionFactory;

	@Autowired
	private DriverManagerDataSource ds;

	@PostConstruct
	private void initialize() {
		try {
			sessionFactory = new org.hibernate.cfg.Configuration()
					.configure()
					.setProperty("hibernate.connection.url", ds.getUrl())
					.setProperty("hibernate.connection.username", ds.getUsername())
					.setProperty("hibernate.connection.password", ds.getPassword())
					.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();
	}

}

@@ -449,16 +449,23 @@
 private 
+static 
 SessionF
@@ -487,16 +487,16 @@
 actory;%0A
-
 %0A%09@Autow
@@ -587,16 +587,47 @@
 ) %7B%0A
+%09%09if (sessionFactory == null)%0A%09
 %09%09try %7B%0A
 %09%09%09s
@@ -622,16 +622,17 @@
 %09%09try %7B%0A
+%09
 %09%09%09sessi
@@ -686,16 +686,17 @@
 ()%0A%09%09%09%09%09
+%09
 .configu
@@ -700,16 +700,17 @@
 igure()%0A
+%09
 %09%09%09%09%09.se
@@ -756,24 +756,25 @@
 s.getUrl())%0A
+%09
 %09%09%09%09%09.setPro
@@ -831,24 +831,25 @@
 ame())%0A%09%09%09%09%09
+%09
 .setProperty
@@ -896,24 +896,25 @@
 Password())%0A
+%09
 %09%09%09%09%09.buildS
@@ -932,16 +932,17 @@
 ry();%0A%09%09
+%09
 %7D catch 
@@ -970,16 +970,17 @@
 x) %7B%0A%09%09%09
+%09
 // Log t
@@ -993,16 +993,17 @@
 eption.%0A
+%09
 %09%09%09Syste
@@ -1068,16 +1068,17 @@
 ex);%0A%09%09%09
+%09
 throw ne
@@ -1112,16 +1112,17 @@
 or(ex);%0A
+%09
 %09%09%7D%0A%09%7D%0A%0A

Update CollectionDAO.java