Announcement Announcement Module
Collapse
No announcement yet.
Session factory has java.lang.NullPointerException after upgrade to hibernate 4 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Session factory has java.lang.NullPointerException after upgrade to hibernate 4

    Hey guys,

    currently I'm migrating to from hibernate 3.6 to hibernate 4.0 after the Spring 3.1 RC1 was released with compatibility.
    My system was working without any problems in version 3.6. But we decided to upgrade to 4.0.

    Now the problem is after I migrated my system to 4.0 that it wont boot anymore. It throws an error when booting.

    PHP Code:
        Caused byorg.springframework.beans.factory.BeanCreationExceptionError creating bean with name 'adminUserDao' defined in ServletContext resource [/WEB-INF/applicationContext-pjadmin.xml]: Cannot resolve reference to bean 'pjAdmin_sessionFactory' while setting bean property 'factory'nested exception is org.springframework.beans.factory.BeanCreationExceptionError creating bean with name 'pjAdmin_sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext-pjadmin.xml]: Invocation of init method failednested exception is java.lang.NullPointerException
         at org
    .springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
         
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
         
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1350)
         
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1111)
         
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:520)
         
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459)
         
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
         
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
         
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
         
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
         
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
         ... 
    25 more
        Caused by
    org.springframework.beans.factory.BeanCreationExceptionError creating bean with name 'pjAdmin_sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext-pjadmin.xml]: Invocation of init method failednested exception is java.lang.NullPointerException
         at org
    .springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1445)
         
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
         
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459)
         
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
         
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
         
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
         
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
         
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
         ... 
    35 more
        Caused by
    java.lang.NullPointerException
         at java
    .util.Hashtable.put(Unknown Source)
         
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.<init>(LocalSessionFactoryBuilder.java:100)
         
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:232)
         
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1504)
         
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1442)
         ... 
    42 more 
    Here below is my configuration of the sessionfactory bean:

    PHP Code:
    <?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-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

        <!-- pjrb_database properties -->

        <bean id="pjAdmin_sessionFactory"
              class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="mappingResources">
                <list>
                    <value>classpath:be/yunico/rbmain/model/AdminUser.hbm.xml</value>
                    <value>classpath:be/yunico/rbmain/model/Subscriptions.hbm.xml</value>
                    <value>classpath:be/yunico/rbmain/model/Price.hbm.xml</value>
                    <value>classpath:be/yunico/rbmain/model/Payment.hbm.xml</value>
                    <value>classpath:be/yunico/rbmain/model/Reduction.hbm.xml</value>
                </list>

            </property>
           <!-- <property name="dataSource" ref="pjadmin_database"/>  -->

            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/pjadmin</prop>
                    <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
                    <prop key="hibernate.connection.username">root</prop>
                    <prop key="hibernate.connection.password">******</prop>
                    <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.spi.JtaPlatform</prop>
                    <prop key="hibernate.connection.release_mode">auto</prop>
                    <prop key="hibernate.transaction.manager_lookup_class">be.yunico.rbmain.util.MyTransactionManagerLookup</prop>
                    <prop key="hibernate.transaction.auto_close_session">true</prop>
                    <prop key="hibernate.transaction.flush_before_completion">true</prop>
                    <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.TransactionManagerLookup</prop>
                    <prop key="jta.UserTransaction">java:comp/UserTransaction</prop>
                    <prop key="hibernate.connection.autocommit">true</prop>
                </props>
            </property>
        </bean>

        <bean id="pjAdminTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="pjAdmin_sessionFactory"/>
            <property name="rollbackOnCommitFailure" value="true"/>
        </bean>

        <tx:annotation-driven transaction-manager="pjAdminTransactionManager"/>

        <tx:advice id="txPjAdminAdvice" transaction-manager="pjAdminTransactionManager">
            <tx:attributes>
                <tx:method name="saveOrUpdate*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
                <tx:method name="delete*" rollback-for="java.lang.Exception"/>
                <tx:method name="get*" read-only="true"/>
            </tx:attributes>
        </tx:advice>



        <!-- pjrb_Dao's -->
        <bean id="adminUserDao" class="be.yunico.rbmain.dao.pjadmin.AdminUserDao"
              scope="singleton">
            <property name="factory" ref="pjAdmin_sessionFactory"/>
        </bean>
    </beans>
    Is there anyone that could point me in the right direction or knows a fix for the migration issue?

    Thanks alot in advance,

    greetz YcKe

  • #2
    I solved the problem,
    the problem was spring wants a datasource bean linked in the LocalSessionFactoryBean.

    Adding a datasource like this:
    PHP Code:
        <bean id="pjadmin_database" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <
    property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <
    property name="url" value="jdbc:mysql://localhost:3306/****"/>
            <
    property name="username" value="root"/>
            <
    property name="password" value="****"/>
        </
    bean
    and this code in your LocalSessionFactoryBean for linking your datasource with the sessionfactory:

    PHP Code:
    <property name="dataSource" ref="pjadmin_database"/> 
    Also something that seemed to be required was the packagesToScan:

    PHP Code:
          <property name="packagesToScan">
                <list>
                    <
    value>com.dummy.yourpackage</value>
                    <
    value>com.dummy.yourpackage</value>
                </list>
    </
    property
    In my opinion just throwing a nullpointer exception is a bad way of handling the errors.
    Maybe they could improve error handling in the final edition?

    Greetz YcKe

    Comment

    Working...
    X