-
Dec 28th, 2011, 09:04 AM
#1
SpringMVC with Hibernate
after executing this application i got HibernateTemplate reference as null but all beans are pre-instantiated properly. whr shuld i change the code,, please help me regarding this,, when i run same application from console based test case(Main method den it works fyn) bt from Controller class when i call DAO method den java.lang.NullPointerException occurs.
org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:659)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:563)
javax.servlet.http.HttpServlet.service(HttpServlet .java:641)
javax.servlet.http.HttpServlet.service(HttpServlet .java:722)
root cause
java.lang.NullPointerException
com.netsol.dao.UserDAOImpl.addUser(UserDAOImpl.jav a:20)
com.netsol.web.UserController.onSubmit(UserControl ler.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.doInvokeMethod(HandlerMethodInv oker.java:710)
org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.invokeHandlerMethod(HandlerMeth odInvoker.java:167)
org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:402)
org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:771)
org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:563)
javax.servlet.http.HttpServlet.service(HttpServlet .java:641)
javax.servlet.http.HttpServlet.service(HttpServlet .java:722)
Below is the spring config file and DAO class
dispatcher-servlet.xml
<?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
="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.Intern alResourceViewResolver"
p
refix="/WEB-INF/jsp/" p:suffix=".jsp" />
<context:component-scan base-package="com.netsol" />
<!-- <jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/globalDataSource" /> -->
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
<property name="jndiName" value="java:comp/env/jdbc/myGlobalDataSource"/>
<property name="resourceRef" value="true" />
</bean>
<!-- <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.0.240:3306/demo"/>
<property name="username" value="java"/>
<property name="password" value="JavA_123#"/>
</bean> -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="annotatedClasses">
<list>
<value>com.netsol.domain.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQ LDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">createOrUpdate</prop>
</props>
</property>
</bean>
<bean id="ud" class="com.netsol.dao.UserDAOImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
</beans>
DAO class:
package com.netsol.dao;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTempla te;
import org.springframework.orm.hibernate3.support.Hiberna teDaoSupport;
import com.netsol.domain.User;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
HibernateTemplate hibernateTemplate;
public UserDAOImpl(){
System.out.println("-----------UserDAOImpl");
}
public boolean addUser(User user){
boolean flag=false;
String s=null;
HibernateTemplate ht=getHibernateTemplate();
ht.save(user).toString();
System.out.println("long value"+s);
if(s!=null)
flag=true;
else
flag=false;
return flag;
}
public User getUser(String keyName){
User user=(User)getHibernateTemplate().get(User.class, keyName);
return user;
}
public List<User> getBulkUsers(){
String queryString="FROM User";
List<User> l=getHibernateTemplate().find(queryString);
return l;
}
public void updateUser(String password,String name){
Query query = this.getSession().createQuery("update User set password= ? where name=?");
query.setParameter(0, password+"%");
query.setParameter(1, name);
int result = query.executeUpdate();
System.out.println("Update User "+result);
}
}
-
Dec 28th, 2011, 10:42 AM
#2
Please use [ code][/code ] tags when posting code.!!!!

1) Don't use HibernateDaoSupport and HibernateTemplate both should be considered deprecated as of hibernate 3.0.1 (about 2007)
2) Your addUser method is flawed.
3) Judging from your stack trace you are creating a new instance yourself instead of using the configured instance from the application context (for a solution please use the search as that question has been answered numerous times before).
-
Dec 28th, 2011, 10:08 PM
#3
reply
i m not creating any new instance just getting the HIbernateTemplate reference from the HibernateDAOSupport class method plzz help me wht shuld i do? where is the error? in my addUser() method,
-
Dec 28th, 2011, 11:10 PM
#4
The getHiberateTemplate() is returning you a non null instance? I hope the User instance you are providing is not null.
Also take your time to go through the entire forum and post your question under the relevant sub forum, this question would have been more appropriate here. This not only keeps the forum clean, but also increases your changes of getting the answer.
-
Dec 28th, 2011, 11:25 PM
#5
User instance is not null, i have checked it,, whr is the problm i m nt understanding?
-
Dec 29th, 2011, 12:01 AM
#6
Couple of more things.
1. Is you application working when you are using the local managed datasource in spring like say the DriverManagerDatasource rather than the one looked up using JNDI?
2. Is your User class having the necessary annotations to make it an Entity. Can you post the User.java code here?
use the [ CODE ] tags while posting the code as Marten pointed out earlier
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules