Post a Job  |   Ask a Question?   |   Already registered? login or register
neha wankhade
07/12/2015 12:28 AM

Issue with Spring MVC and Hibernate Intergation using Spring ORM


developing application where i m using spring mvc and hibernate using spring orm. when i m injecting HibernateTamplate in DAO class using constructor injection, then getting exception java.lang.NoSuchMethodException: com.imp.doa.LoginDao.<init>() ; because dispacterServlet create object, with default constructor. so i removed parameterized constructor , then injected HibernateTemplate using setter injection , but hibernateTemplate object is not created, getting java.lang.NullPointerException. but when same thing i tried with without spring mvc , then its working. but after Integration with spring mvc , dispatacherServlate can't create object with Parameterized constructor . below are config file and entity class and dao class . persistence-beans.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/mpmagt" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value>com.imp.model.Login</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="show_sql">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" read-only="false" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.imp.service.*.*(..))" id="txPointcut" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config> <bean id="loginDao" class="com.imp.doa.LoginDao"> <property name="hibernateTemplate" ref="hibernateTemplate"/> </bean> </beans> DAO class : LoginDao.java package com.imp.doa; import java.util.List; import org.hibernate.type.TrueFalseType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Required; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository; import com.imp.model.Login; @Repository public class LoginDao { private HibernateTemplate hibernateTemplate; //private final String GET_LOGIN_DETAILS = "FROM Login where username=:? and password=:? "; public List isUserExist(Login login) { System.out.println("inside isUserExit()"); System.out.println(hibernateTemplate); // Getting null when constructor injection List<Login> list = hibernateTemplate.find("From Login"); System.out.println("dao executeed"); return null; } public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } } Enityclass : Login.java package com.imp.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table public class Login { // pid, username, password private int pid; private String username; private String password; public Login() { System.out.println("login Entity"); } @Id @GeneratedValue public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } @Column public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } Exception After Setter Injection : Exception due to after Setter Injection : SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/IMPmgts] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException Exception after constructor injection : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.imp.service.ILogin com.imp.controller.LoginController.iLogin; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.imp.doa.LoginDao com.imp.service.LoginService.loginDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginDao' defined in file [D:\madhur-prac\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\IMPmgts\WEB-INF\classes\com\imp\doa\LoginDao.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.imp.doa.LoginDao]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.imp.doa.LoginDao.<init>()
Spam    2 Comments

rabindra,jain
01/01/2016 12:49:46
You are not doing DI in all layers properly.

rabindra,jain
01/01/2016 12:49:48
You are not doing DI in all layers properly.

Languages :
xml