Announcement Announcement Module
Collapse
No announcement yet.
NPE when I will test my injected DAOs Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NPE when I will test my injected DAOs

    Hi!
    I am using Spring 3.0.5, Hibernate 3.6.0, JUnit 4.8.2 and Maven 3.0.
    I am trying to inject my dao and test it.

    I get NPE in this.fastighetUuidDao.save(fastighetUUID); and fastighetUuidDao is null

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @TestExecutionListeners()
    @ContextConfiguration(locations = {"classpath:/Meta-INF/spring/spring-master.xml"})
    //@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
    @TransactionConfiguration(transactionManager = "milinsTransactionManager", defaultRollback= false)
    @Transactional
    public class TestFastighetUuidDaoHibernate {
    	
    	@Autowired
    	FastighetUuidDao fastighetUuidDao;
    	
    	@Test
    	@Transactional
    	public void testSave() {
    		
    		Assert.assertNotNull(fastighetUuidDao);
    		FastighetUUID fastighetUUID = new FastighetUUID();
    		fastighetUUID.setFnr("123456");
    		fastighetUUID.setUuid("0987654");
    		this.fastighetUuidDao.save(fastighetUUID);
    		Assert....
    		
    	}
    ...
    }
    =====================
    My FastighetUuidDao interface:
    Code:
    public interface FastighetUuidDao extends GenericDao<FastighetUUID> {
    	
    	public void saveAll(List<FastighetUUID> list) throws DataAccessException;
    }
    and the class i implement this interface:
    Code:
    @Repository("fastighetUuidDao")
    public class FastighetUuidDaoHibernate 
    			extends GenericDaoHibernate<FastighetUUID> 
    			implements FastighetUuidDao {
    
    	public FastighetUuidDaoHibernate() {
    		super(FastighetUUID.class);
    	}
    
    	@Override
    	public void saveAll(List<FastighetUUID> list) throws DataAccessException {
    		for (FastighetUUID fastighetUuid : list) {
    			this.save(fastighetUuid);
    		}
    
    	}
    	
    	@Autowired
    	@Qualifier("milinsSessionFactory")
    	public void setupSessionFactory(SessionFactory sessionFactory) {
    		this.setSessionFactory(sessionFactory);
    	}
    }
    and the GenericDao<T> is:
    Code:
    public class GenericDaoHibernate<T extends DomainObject> extends
    		HibernateDaoSupport implements GenericDao<T> {
    	
    	private Class<T> type;
    
    	public GenericDaoHibernate(Class<T> type) {
    		super();
    		this.type = type;
    	}
    
    .....
    	
    	@Autowired
    //	@Qualifier("milinsDataSource")
    	public void setupSessionFactory(SessionFactory sessionFactory) {
    		this.setSessionFactory(sessionFactory);
    	}
    
    }
    ====================
    I have a spring-master.xml:
    Code:
    ....
    	<context:component-scan base-package="se.lantmateriet.milins">
    <!--		<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>-->
    		<context:exclude-filter expression="org.springframework.stereotype.Repository" type="annotation"/>
    	</context:component-scan>
    	
        <!-- This will ensure that hibernate or jpa exceptions are automatically translated into
             Spring's generic DataAccessException hierarchy -->
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    	
    	<import resource="spring-datasource.xml"/>
    	
    	<!--We should import spring-jpa.xml if we would like to use JPA (EntityManager)-->
    	<import resource="spring-hibernate.xml" />
    
    ....
    and my Spring-datasource.xml is:
    ============
    Code:
    ...
        <bean id="milinsDataSource"          
              autowire-candidate="false"
              class="org.springframework.jdbc.datasource.SingleConnectionDataSource"
              p:driverClassName="${jdbc.driverclassname}"
              p:url="${milins.jdbc.url}"
              p:username="${milins.jdbc.username}"
              p:password="${milins.jdbc.password}"/>
              
        <bean id="levfrDataSource"          
              autowire-candidate="false"
              class="org.springframework.jdbc.datasource.SingleConnectionDataSource"
              p:driverClassName="${jdbc.driverclassname}"
              p:url="${levfr.jdbc.url}"
              p:username="${levfr.jdbc.username}"
              p:password="${levfr.jdbc.password}"/>
    ...
    ============

    and spring-hibernate.xml is:

    ============
    Code:
    ...
        <!-- Milins Hibernate SessionFactory -->
        <bean id="milinsSessionFactory" 
        	  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
              p:dataSource-ref="milinsDataSource">
            <property name="annotatedClasses">
                <list>
                    <value>se.lantmateriet.milins.domain.inskrivning.IrFastighet</value>
                    <value>se.lantmateriet.milins.domain.inskrivning.GroupInfo</value>
                    <value>se.lantmateriet.milins.domain.inskrivning.FastighetUUID</value>
                </list>
            </property>
    
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=${hibernate.dialect}
                    hibernate.show_sql=${hibernate.show_sql}
                    hibernate.format_sql=${hibernate.format_sql}
                    hibernate.transaction.factory_class=${hibernate.transaction.factory_class}                
                    hibernate.cache.provider_class=${hibernate.cache.provider_class}
                    hibernate.hbm2ddl.auto=update
                    hibernate.cache.use_second_level_cache=true
                    hibernate.cache.use_query_cache=true
                    hibernate.search.default.directory_provider=${hibernate.search.default.directory_provider}
                    hibernate.search.default.indexBase=${hibernate.search.default.indexBase}
                    hibernate.search.default.batch.merge_factor=${hibernate.search.default.batch.merge_factor}
                    hibernate.search.default.batch.max_buffered_docs=${hibernate.search.default.batch.max_buffered_docs}
                </value>
            </property>
        </bean>
        
        <!-- LevFR Hibernate SessionFactory -->
        <bean id="levfrSessionFactory"
        	  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
              p:dataSource-ref="levfrDataSource">
            <property name="annotatedClasses">
                <list>
                    <value>se.lantmateriet.milins.domain.inskrivning.LevfrFastighetUuid</value>
                </list>
            </property>
    
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=${hibernate.dialect}
                    hibernate.show_sql=${hibernate.show_sql}
                    hibernate.format_sql=${hibernate.format_sql}
                    hibernate.transaction.factory_class=${hibernate.transaction.factory_class}                
                    hibernate.cache.provider_class=${hibernate.cache.provider_class}
                    hibernate.hbm2ddl.auto=update
                    hibernate.cache.use_second_level_cache=true
                    hibernate.cache.use_query_cache=true
                    hibernate.search.default.directory_provider=${hibernate.search.default.directory_provider}
                    hibernate.search.default.indexBase=${hibernate.search.default.indexBase}
                    hibernate.search.default.batch.merge_factor=${hibernate.search.default.batch.merge_factor}
                    hibernate.search.default.batch.max_buffered_docs=${hibernate.search.default.batch.max_buffered_docs}
                </value>
            </property>
        </bean>
    
        <!-- Read in DAOs from the hibernate package -->
        <context:component-scan base-package="se.lantmateriet.milins.dao.hibernate"/>
    
        <!-- Transaction Config -->
        <bean id="milinsTransactionManager"
              class="org.springframework.orm.hibernate3.HibernateTransactionManager"
              p:sessionFactory-ref="milinsSessionFactory"/>
    
        <bean id="levfrTransactionManager"
              class="org.springframework.orm.hibernate3.HibernateTransactionManager"
              p:sessionFactory-ref="levfrSessionFactory"/>
    
    
        <tx:annotation-driven transaction-manager="milinsTransactionManager"/>
        
        <tx:annotation-driven transaction-manager="levfrTransactionManager"/>
    ...
    ============

    I cannot se what is my fault... everything seems to be correct.
    Would anybody please help me to find the problem in injection?

    Best Regards
    Sheri
    Last edited by Sheri; Nov 24th, 2010, 08:27 AM. Reason: using [code]..[/code]

  • #2
    Please use [ code][/code ] tags, they exist for a reason ...

    You aren't registering any TestExecutionListeners so no dependency injection. I suggest removing that annotation...

    Comment


    • #3
      Sorry for the mistake regarding missing of [ code][/code ], I have fix it
      I removed the annotation, got another error. I will try to solve it and come back later about the result.

      Originally posted by Marten Deinum View Post
      Please use [ code][/code ] tags, they exist for a reason ...

      You aren't registering any TestExecutionListeners so no dependency injection. I suggest removing that annotation...

      Comment


      • #4
        Now I am trying to solve another problem, as I see in my code, everything seems to be in its place but I get the following error:

        TransactionFactory class not found: org.hibernate.transaction.JDBCTransactionFactory

        Code:
        ...
        Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'milinsSessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: TransactionFactory class not found: org.hibernate.transaction.JDBCTransactionFactory                
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
        	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
        	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:398)
        	at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:275)
        	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:139)
        	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:79)
        	at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:70)
        	at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:99)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1439)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1408)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
        	... 35 more
        Caused by: org.hibernate.HibernateException: TransactionFactory class not found: org.hibernate.transaction.JDBCTransactionFactory                
        	at org.hibernate.transaction.TransactionFactoryFactory.buildTransactionFactory(TransactionFactoryFactory.java:69)
        	at org.hibernate.cfg.SettingsFactory.createTransactionFactory(SettingsFactory.java:463)
        	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:174)
        	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)
        	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)
        	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
        	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
        	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
        	at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
        ....
        Any idea?

        Comment


        • #5
          Judging from the stacktrace not everything is in place... You seem to be missing some crucial jars on your classpath.

          Comment


          • #6
            I think I know the problem! Since I use Hibernate 3.6.0 and Hibernate Search 3.2.1 there should be some conflicts.
            I have commented "hibernate.transaction.factory_class=${hibernate.t ransaction.factory_class}" in my hibernate properties since I think I do not this property.

            If there is someone who can help me more I will appreciate.

            BR
            Sheri

            Comment


            • #7
              Thanks Marten! I should double check my Maven pom. Really confused in version compatibility

              Originally posted by Marten Deinum View Post
              Judging from the stacktrace not everything is in place... You seem to be missing some crucial jars on your classpath.

              Comment


              • #8
                Hibernate and getting the different hibernate versions/frameworks working together can be quite a painful experience.

                Comment

                Working...
                X