Announcement Announcement Module
Collapse
No announcement yet.
Spring Web Application getting NullPointerException while hibernateTemplate.load() Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Web Application getting NullPointerException while hibernateTemplate.load()

    Hi,

    I'm developing a Spring Rest Webservice with Hibernate Annotations. The application is deployed to a GlassFish 2.1.1.

    Deployment works fine. The test-service-method (returning just a simple String), also. But when I try to get an object from the database I get a NullpointerException at org.hibernate.impl.SessionImpl.get(SessionImpl.jav a:809).

    Google doesn't get me further.

    Stacktrace from server.log:

    Code:
    java.lang.NullPointerException
    	at org.hibernate.impl.SessionImpl.get(SessionImpl.java:809)
    	at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:519)
    	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    	at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    	at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512)
    	at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506)
    	at de.wondrak.shopService.dataaccess.hibernate.GenericDaoImpl.load(GenericDaoImpl.java:50)
    	at de.wondrak.shopService.service.ShopServiceImpl.loadCustomer(ShopServiceImpl.java:17)
    	at de.wondrak.shopService.ShopServiceController.getCustomer(ShopServiceController.java:29)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    	at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:315)
    	at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
    	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
    	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
    	at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
    	at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
    	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
    	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
    	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
    	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1093)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166)
    	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
    	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
    	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
    	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1093)
    	at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:291)
    	at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:666)
    	at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:597)
    	at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:872)
    	at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
    	at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
    	at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
    	at com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTask.doTask(PortUnificationPipeline.java:382)
    	at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:264)
    	at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
    Hope somebody got a similar problem.

    Greetz
    Markus

  • #2
    This is my dataaccess.xml:

    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    	<bean name="shopService" class="de.wondrak.shopService.service.ShopServiceImpl" >
    		<property name="kundeDao" ref="kundeDao"></property>
    	</bean>
    	
    
    	<bean name="artikelDao"
    		class="de.wondrak.shopService.dataaccess.hibernate.ArtikelDaoImpl"
    		parent="genericDao"></bean>
    	<bean name="kundeDao"
    		class="de.wondrak.shopService.dataaccess.hibernate.KundeDaoImpl"
    		parent="genericDao"></bean>
    
    	<bean name="genericDao"
    		class="de.wondrak.shopService.dataaccess.hibernate.GenericDaoImpl">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="serverDataSource" />
    		<property name="configLocation" value="WEB-INF/classes/hibernate.cfg.xml" />
    
    	</bean>
    
    	<bean id="serverDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName" value="jdbc/mysql.shop" />
    	</bean>
    	
    	<bean id="mySQLDataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/shop" />
    		<property name="username" value="shopuser" />
    		<property name="password" value="start123" />
    	</bean>
    </beans>
    There are two datasources defined, but both cause the same Error.

    Comment


    • #3
      It might be useful to see your GenericDaoImpl class, the entity class(es) you're attempting to query, and the excerpt from the hibernate.cfg.xml for those entities.

      I don't know what version of Hibernate you're using. You have access to the Hibernate source, so you should be able to tell what variable is null at that point. That could be a useful clue.

      Comment


      • #4
        Thanks for the reply.

        Im using
        spring 3.0.0.RELEASE
        hibernate 3.2.7.ga
        hibernate-annotations 3.2.1.ga

        To debug this error I have no time now, but I will post it tomorrow.

        The wanted sources as follows:

        GenericDAOImpl:
        Code:
        package de.wondrak.shopService.dataaccess.hibernate;
        
        import java.io.Serializable;
        import java.util.List;
        
        import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
        
        import de.wondrak.shopService.dataaccess.GenericDao;
        
        public class GenericDaoImpl<t> extends HibernateDaoSupport implements
        		GenericDao<t> {
        
        	/**
        	 * Instance of the class-object, this dao is handling.
        	 * 
        	 * @see {@link this.setClazz()}
        	 * @see <a
        	 *      href="http://forum.springframework.org/showthread.php?t=19008">(Spring-Forum
        	 *      Beitrag über Generische DAOs)</a>
        	 */
        	private Class<t> clazz;
        
        	public void setClazz(Class<t> clazz) {
        		this.clazz = (Class<t>) clazz;
        	}
        
        	public GenericDaoImpl() {
        
        	}
        
        	public void delete(t entity) {
        		getHibernateTemplate().delete(entity);
        	}
        
        	public List<t> findByExample(t entity) {
        		return getHibernateTemplate().findByExample(entity);
        	}
        
        	public t load(Serializable id) {
        		return getHibernateTemplate().get(clazz, id);
        	}
        
        	public List<t> loadAll() {
        		return getHibernateTemplate().loadAll(clazz);
        	}
        
        	public void save(t entity) {
        		getHibernateTemplate().save(entity);
        
        	}
        
        }
        The Kunde-entity is the only entity im working with, at this time:

        package de.wondrak.shopService.domain;

        Code:
        import javax.persistence.Column;
        import javax.persistence.Entity;
        import javax.persistence.GeneratedValue;
        import javax.persistence.Id;
        import javax.persistence.Table;
        
        @Entity
        @Table(name = "kunde")
        public class Kunde {
        
        	@Id
        	@GeneratedValue
        	@Column(name = "userid")
        	private int id;
        
        	@Column(name = "name", nullable = false)
        	private String name;
        
        	@Column(name = "vorname", nullable = false)
        	private String vorname;
        
        	@Column(name = "strasse", nullable = false)
        	private String strasse;
        
        	@Column(name = "ort", nullable = false)
        	private String ort;
        
        	@Column(name = "plz", length = 6, nullable = false)
        	private int plz;
        
        	@Column(name = "telefon")
        	private String telefon;
        
        	public Kunde() {
        		
        	}
        	
        	public int getId() {
        		return id;
        	}
        
        	public void setId(int id) {
        		this.id = id;
        	}
        
        	public String getName() {
        		return name;
        	}
        
        	public void setName(String name) {
        		this.name = name;
        	}
        
        	public String getVorname() {
        		return vorname;
        	}
        
        	public void setVorname(String vorname) {
        		this.vorname = vorname;
        	}
        
        	public String getStrasse() {
        		return strasse;
        	}
        
        	public void setStrasse(String strasse) {
        		this.strasse = strasse;
        	}
        
        	public String getOrt() {
        		return ort;
        	}
        
        	public void setOrt(String ort) {
        		this.ort = ort;
        	}
        
        	public int getPlz() {
        		return plz;
        	}
        
        	public void setPlz(int plz) {
        		this.plz = plz;
        	}
        
        	public String getTelefon() {
        		return telefon;
        	}
        
        	public void setTelefon(String telefon) {
        		this.telefon = telefon;
        	}
        
        }
        hibertnate.cfg.xml:

        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-configuration PUBLIC
        		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
        <hibernate-configuration>
        	<session-factory>
        		<property name="dialect">
        			org.hibernate.dialect.MySQLInnoDBDialect
              </property>
        		<property name="cache.provider_class">
        			org.hibernate.cache.EhCacheProvider
              </property>
        		<property name="jdbcCompliantTruncation">false</property>
        		<property name="hbm2ddl.auto">none</property>
        		<!-- <mapping class="de.wondrak.shopService.domain.Artikel" />  -->
        		<mapping class="de.wondrak.shopService.domain.Kunde" />
        	</session-factory>
        </hibernate-configuration>
        Greetz
        Markus

        Comment


        • #5
          Problem fixed.

          In my extended classes of the GenericDAOImpl I had to set the handled domain class. The property clazz was null, so the entity was null, too.

          Thx
          Markus

          Comment

          Working...
          X