Announcement Announcement Module
Collapse
No announcement yet.
Newbie. Hibernate + MySQL Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie. Hibernate + MySQL

    Newbie question of the day...

    I'm getting an NPE whenever calling getHibernateTemplate().get(Address.class, new Long(addressId));

    Any pointers helpfull.

    Error
    Code:
    java.lang.NullPointerException
    	org.aafp.tracker.dao.impl.AddressDaoImpl.loadAddress(AddressDaoImpl.java:20)
    	org.aafp.tracker.web.TrackerController.handleRequest(TrackerController.java:27)
    	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:522)
    	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:321)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    	org.springframework.orm.hibernate.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:170)
    	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:73)

    applicationContext.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
    	<!-- ========================= GENERAL DEFINITIONS ========================= -->
    	
    	<!-- Message source for this context, loaded from localized "messages_xx" files -->
    	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basename"><value>messages</value></property>										
    	</bean>
    	
    	
    	<!-- ========================= RESOURCE DEFINITIONS ========================= -->
    	
    	<!-- Local DataSource that works in any environment -->
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
    		<property name="url"><value>jdbc&#58;mysql&#58;//127.0.0.1&#58;3306/tracker?autoReconnect=true</value></property>
    		<property name="username"><value>nobody</value></property>
    		<property name="password"><value></value></property>
    	</bean>
    	
    	<!-- Hibernate Date Interceptor -->
    	<bean id="hibernateDateInterceptor" class="org.aafp.tracker.util.HibernateDateInterceptor"/>
    	
    	<!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    		<property name="dataSource"><ref local="dataSource"/></property>
    		<!-- <property name="entityInterceptor"><ref bean="hibernateDateInterceptor"/></property> -->
    		<property name="mappingResources">
    			<list>
    				<value>org/aafp/tracker/domain/address.hbm.xml</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
    				<prop key="hibernate.show_sql">true</prop> 
    			</props>
    		</property>
    	</bean>
    	
    	<!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
    	<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    	</bean>
    	
    	
    	<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
    	
    	<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    			abstract="true">
    		<property name="transactionManager"><ref bean="transactionManager"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="store*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<bean id="addressDao" parent="baseTransactionProxy">
    		<property name="target">
    			<bean class="org.aafp.tracker.dao.impl.AddressDaoImpl">
    				<property name="sessionFactory"><ref local="sessionFactory"/></property>
    			</bean>
    		</property>
    	</bean>
    
    </beans>
    AddressDaoImpl
    Code:
    public class AddressDaoImpl extends HibernateDaoSupport implements AddressDao &#123;
        public Address loadAddress&#40;long addressId&#41; &#123;
            return &#40;Address&#41; getHibernateTemplate&#40;&#41;.get&#40;Address.class, new Long&#40;addressId&#41;&#41;;
        &#125;
    &#125;

  • #2
    The HibernateTemplate.get() method returns null if there is no entity for the given PK. Are you sure you have data for that PK in the database? HibernateTemplate.load() throws an exception if not found. See the Javadoc on the HibernateOperations interface.

    Comment


    • #3
      Both HibernateTemplate.get() and HibernateTemplate.load() throw the NPE.

      This also throws the NPE, which leads me to believe there's something wrong with my config.

      Code:
      public class AddressDaoImpl extends HibernateDaoSupport implements AddressDao &#123;
          public Address loadAddress&#40;long addressId&#41; &#123;
              // Just call the method to see if the problem is in there.
              getHibernateTemplate&#40;&#41;.get&#40;Address.class, new Long&#40;addressId&#41;&#41;;
      
              // Make sure we returning a non null Address.
              return new Address&#40;&#41;;
          &#125;
      &#125;
      Thanks for the help.

      Comment


      • #4
        Here's some more of my config in hoping someone spots something wrong.

        address.hbm.xml
        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD//EN"
            "http&#58;//hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
        
        <hibernate-mapping>
            <class 
                name="org.aafp.tracker.domain.Address" 
                table="addresses">
                
                <id 
                    name="addressId" 
                    type="long"
                    column="address_id" 
                    unsaved-value="0">
                    <generator class="native"/>
                </id>
                
                <property name="city"
                          column="city"
                          type="string"/>
                <property name="state"
                          column="state"
                          type="string"/>
                <property name="zip"
                          column="zip"
                          type="string"/>
                <property name="address1"
                          column="address1"
                          type="string"/>
                <property name="address2"
                          column="address2"
                          type="string"/>
                <property name="address3"
                          column="address3"
                          type="string"/>
                <property name="primary"
                          column="is_primary"
                          type="boolean"/>
            </class>
        </hibernate-mapping>
        If there's anything else I can post that would be helpful, please let me know.

        TIA

        Comment


        • #5
          Full stack trace of the NPE for those playing along at home.
          Code:
          2004-10-24 16&#58;22&#58;24,617 ERROR &#91;org.springframework.web.servlet.DispatcherServlet&#93; - <Could not complete request>
          java.lang.NullPointerException
                  at org.aafp.tracker.dao.impl.AddressDaoImpl.loadAddress&#40;AddressDaoImpl.java&#58;14&#41;
                  at org.aafp.tracker.web.TrackerController.handleRequest&#40;TrackerController.java&#58;27&#41;
                  at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle&#40;SimpleControllerHandlerAdapter.java&#58;44&#41;
                  at org.springframework.web.servlet.DispatcherServlet.doService&#40;DispatcherServlet.java&#58;522&#41;
                  at org.springframework.web.servlet.FrameworkServlet.service&#40;FrameworkServlet.java&#58;321&#41;
                  at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;802&#41;
                  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;237&#41;
                  at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;157&#41;
                  at org.springframework.orm.hibernate.support.OpenSessionInViewFilter.doFilterInternal&#40;OpenSessionInViewFilter.java&#58;170&#41;
                  at org.springframework.web.filter.OncePerRequestFilter.doFilter&#40;OncePerRequestFilter.java&#58;73&#41;
                  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;186&#41;
                  at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;157&#41;
                  at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperValve.java&#58;214&#41;
                  at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
                  at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
                  at org.apache.catalina.core.StandardContextValve.invokeInternal&#40;StandardContextValve.java&#58;198&#41;
                  at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextValve.java&#58;152&#41;
                  at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
                  at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
                  at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.java&#58;137&#41;
                  at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
                  at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.java&#58;117&#41;
                  at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;102&#41;
                  at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
                  at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineValve.java&#58;109&#41;
                  at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
                  at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
                  at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;929&#41;
                  at org.apache.coyote.tomcat5.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;160&#41;
                  at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java&#58;799&#41;
                  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection&#40;Http11Protocol.java&#58;705&#41;
                  at org.apache.tomcat.util.net.TcpWorkerThread.runIt&#40;PoolTcpEndpoint.java&#58;577&#41;
                  at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadPool.java&#58;683&#41;
                  at java.lang.Thread.run&#40;Thread.java&#58;534&#41;

          Comment


          • #6
            Thought I would close out this thread. The problem was a configuration one. I wasn't injecting the AddressDao into my controller, but instantiating instead. Once I fixed that everything is working.

            Comment


            • #7
              Hello,

              I am having the same problem. Can you explain in detail what the config problem was and how you fixed it?


              Thanks

              Comment

              Working...
              X