Monday, September 30, 2013

A Simple Abstract DAO Class with a Simple Abstract Spring Configuration File. -> Goodbye to all that centrism

DAOBagTeste
NFL

Next version will be fully function i hope soul.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <import resource="classpath:database-loaders/properties_config.xml"/> <bean id="tikaDataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.tika.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="IndexConfigDAO" class="org.nanotek.dao.GeneralPurposeDAO"> <property name="sessionFactory" ref="sessionFactory"></property> <property name="clazz" value="org.nanotek.cms.domain.lucene.config.IndexConfig"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="tikaDataSource" /> <property name="mappingLocations" value="classpath*:/conf/**/IndexConfig.hbm.xml" /> <property name="hibernateProperties"> <value> <![CDATA[ hibernate.show_sql=false hibernate.format_sql=false hibernate.hbm2ddl.auto=update ]]> </value> </property> </bean> <bean id="indexConfigSpringBuilder" class="org.nanotek.tests.hibernate.IndexConfigSpringBuilder"> </bean> <bean id="indexConfig" class="org.nanotek.cms.domain.lucene.config.IndexConfig"> </bean> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" lazy-init="false"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven proxy-target-class="true" transaction-manager="hibernateTransactionManager"/> </beans>


package org.nanotek.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.nanotek.util.Base;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public abstract class BaseDAO {

        protected Class<Base<?>> clazz;  
        @Autowired
        private SessionFactory sessionFactory;
       
       
        public BaseDAO() {
        }

        public BaseDAO(Class<Base<?>> clazz) {
                this.clazz = clazz;
        }
       
        @Transactional (readOnly = true, propagation = Propagation.REQUIRED)
        public List<Base<?>> loadAll() {
                Session session = sessionFactory.getCurrentSession();
                return session.createQuery("from " + getClazz().getName()).list();
        }

        @Transactional (readOnly = false, propagation = Propagation.REQUIRED)
        public void persist(Base<?> object) {
                Session session = sessionFactory.getCurrentSession();
                session.persist(object);
        }

        @Transactional (readOnly = false, propagation = Propagation.REQUIRED)
        public void update(Base<?> object) {
                Session session = sessionFactory.getCurrentSession();
                session.saveOrUpdate(object);
        }

        @Transactional (readOnly = false, propagation = Propagation.REQUIRED)
        public void delete(Base<?> object) {
                Session session = sessionFactory.getCurrentSession();
                session.delete(object);
        }

        public Class<Base<?>> getClazz() {
                return clazz;
        }

        public void setClazz(Class<Base<?>> clazz) {
                this.clazz = clazz;
        }
       
        @Transactional
        public abstract List<Base<?>> listRecords(Integer firstResult, Integer maxResults);

        public SessionFactory getSessionFactory() {
                return sessionFactory;
        }

        public void setSessionFactory(SessionFactory sessionFactory) {
                this.sessionFactory = sessionFactory;
        }

}