Announcement Announcement Module
Collapse
No announcement yet.
Several DAOs extending from one BaseDAOClass Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Several DAOs extending from one BaseDAOClass

    I asked this question together, but nobody answer. I'll try to improve my question.

    I have a BaseDAOClass called:

    Code:
    public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {
        
        public boolean inserir(Object o) {
            getHibernateTemplate().save(o);
            return true;
        }
        public Object alterar(Object o) {
            getHibernateTemplate().update(o);
            return o;
        }
        public boolean excluir(Object o) {
            getHibernateTemplate().delete(o);
            return true;
        }
        public Object selecionar(Integer pk) {
            Object o =getHibernateTemplate().load(getClasseVO(),pk);
            return o;
        }
        public Collection listar() {
            return getHibernateTemplate().find("from "+getClasseVO().getName());
            
        }
        
        
        
        protected  Class getClasseVO() {
            try {
                String classe = getClass().toString();
                classe = classe.replaceAll("dao", "classe");
                classe = classe.substring(6, classe.length() - 7);
                return Class.forName(classe);
            } catch (ClassNotFoundException e) {
                throw new HibernateException(e);
            }
        }
    }

    My DAOs extend from BaseDAOImpl:

    Code:
    public class SistemaDAOImpl extends BaseDaoImpl implements SistemaDAO {
    
    	
    	
        public Object alterar(Object o) {
            
            return super.alterar(o);
        } 
    
        public boolean excluir(Object o) {
           
            return super.excluir(o);
        }
    
        protected Class getClasseVO() {
           
            return super.getClasseVO();
        }
    
        public boolean inserir(Object o) {
           
            return super.inserir(o);
        }
    
        public Collection listar() {
            
            return super.listar();
        }
    
        public Object selecionar(Integer pk) {
            
            return super.selecionar(pk);
        }
    
        
        
    }
    My spring-hibernate.xml

    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="configLocation">
        		<value>hibernate.cfg.xml</value>
    		</property>
    	</bean>
    	
    	<bean id="transactionManager"            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    	<bean id="comarcaDAO" class="br.gov.pa.tj.comarca.dao.ComarcaDAOImpl">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    	</bean>
    
    <bean id="sistemaDAO" class="br.gov.pa.tj.sistema.dao.SistemaDAOImpl">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    	</bean>
    	<bean id="sistemaFacadeImpl" class="br.gov.pa.tj.sistema.facade.SistemaFacadeImpl">
    		<property name="sistemaDAO"> 
    			<ref bean="sistemaDAO"/>
    		</property>
    	</bean>
    	<bean id="sistemaFacade"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager" ref="transactionManager"/>
    		<property name="target" ref="sistemaFacadeImpl"/>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="inserir*">PROPAGATION_REQUIRED</prop>
    				<prop key="alterar*">PROPAGATION_REQUIRES_NEW</prop>
    				<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
    			</props>
    		</property>
    	</bean>




    My questions:

    Is this a good approach (Several DAOs extending from one BaseDAOClass)?

    Am i right to inject a sessionFactory in each DAO?

    Regards

  • #2
    Not sure what benefit the inheritence hierarchy gives you, but sure, there is nothing *wrong* with what you are doing.

    Not sure what you are gaining though

    Yes you do need to inject the sessionFactory.

    Also,
    Code:
        protected  Class getClasseVO&#40;&#41; &#123;
            try &#123;
                String classe = getClass&#40;&#41;.toString&#40;&#41;;
                classe = classe.replaceAll&#40;"dao", "classe"&#41;;
                classe = classe.substring&#40;6, classe.length&#40;&#41; - 7&#41;;
                return Class.forName&#40;classe&#41;;
            &#125; catch &#40;ClassNotFoundException e&#41; &#123;
                throw new HibernateException&#40;e&#41;;
            &#125;
        &#125;
    looks very dangerous. What are you trying to do?

    Comment


    • #3
      Facades

      Hello, I would like so much to see your Facades and FacadeImpl interface/classes. Could you put them here for us?

      Regards,
      Alisson Patrick
      SCJP, SCWCD

      Comment

      Working...
      X