0

I'm new to spring and hibernate.I've created a project and here is what it does. The application has the form elements in JSP which redirects to the Controller class after pressing submit. The controller calls the service layer which in turn calls DAO and finally adds the records to the data base. The issue I am having is that in the Controller class, I can receive the request parameters (POST) but the thing is that there is an issue while calling the addMember() of the service layer. The issue is with the Null Pointer Exception on object. Could you guys review the code and provide me suggestions?

here is my controller class LoginController

package com.online.site.member;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.online.site.Member.Member;
import com.online.site.Member.MemberBoImpl;

@Controller
@RequestMapping("/Login.htm")
public class LoginController {
@Autowired
//@Qualifier(value="memberServiceImp")
private MemberBoImpl memberServiceImpl;


    public void setMemberServiceImp(MemberBoImpl memberServiceImpl) {
    this.memberServiceImpl = memberServiceImpl;
    System.out.println(memberServiceImpl.toString());
}

    @RequestMapping(method=RequestMethod.POST)
    public String executeLogin(@ModelAttribute("Login") Member member, BindingResult result)
    {
        /* call member record fetch method from service layer.
         * if login successful redirect to home page. If not a member 
         * call create member method from service layer and redirect to create account page*/
        memberServiceImpl.addMember(member);
        //memberServiceImp.getMemberDetails(member.getMemberId());
        return "CreateMemberProfile";
    }

    @RequestMapping(method=RequestMethod.GET)
    public String initializeMemberLoginForm(ModelMap model)
    {
        Member member = new Member();
        model.addAttribute("Login", member);
        return "Login";


    }
}

Here is the service class

@Service
public class MemberBoImpl implements MemberInterface{
//@Autowired
    private MemberDaoImpl memberDaoImpl;
/*private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;}*/

    public void setMemberDaoImpl(MemberDaoImpl memberDaoImpl) {
        this.memberDaoImpl = memberDaoImpl;
        System.out.println("in doa setter");
    }

    public boolean addMember(Member member) {
        //hibernateTemplate.save(member);
        System.out.println("in bo impl"+member.getMemberId());
        System.out.println(memberDaoImpl.toString());
        memberDaoImpl.addMember(member);
        return false;
    }
}

Here is DAO class

package com.online.site.Member;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class MemberDaoImpl implements MemberInterface{
@Autowired
    private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
        System.out.println("in hibernate setter");}

    public boolean addMember(Member member) {
        System.out.println("dao impl");
        hibernateTemplate.save(member);
        return false;
    }


}

here is config.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:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd">

    <context:annotation-config/>

<!--    <context:component-scan base-package="com.online.site"></context:component-scan> -->

    <bean id="memberServiceImpl" class="com.online.site.Member.MemberBoImpl"
        scope="singleton" autowire="byType">
     <property name="memberDaoImpl" ref="memberDaoImpl"></property>
 <!--  <property name="hibernateTemplate" ref="hibernateTemplate"></property>-->
    </bean>

    <bean id="member" class="com.online.site.Member.Member"/>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/onlineshopssite"></property>
        <property name="username" value="root"></property>
        <property name="password" value="admin"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource"></property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>

        <property name="annotatedClasses">
            <list>
                <value>com.online.site.Member.Member</value>
            </list>
        </property>
    </bean>

    <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
     <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

       <bean id="memberDaoImpl" class="com.online.site.Member.MemberDaoImpl" autowire="byType">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
</beans>

Here is 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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath*:/onlineShop-config.xml" />


    <!-- step eight: http://localhost:8080/Spring3MVCDemo/customer.htm -->
<context:component-scan base-package="com.online.site,com.online.site.Billing,com.online.site.Member"></context:component-scan>


    <!-- step 12 public String initializeCustomerForm(ModelMap model) { returning 
        string value return "createCustomer" createCustomer prefix and suffix will 
        be added by the InternalResourceViewResolver http://localhost:8080/Spring3MVCDemo/createCustomer.jsp -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />

    </bean>

</beans>

I've tried many suggestions on various forums but nothing seems to work.I'm very likely missing some basci step. Can anyone please take a look at my code and suggest?

User123
  • 1
  • 3
  • Also, if you're using field injection, you don't need the setters (unless you're planning on changing `HibernateTemplate`s somewhere in your business logic, which seems unlikely). – Floegipoky Nov 05 '14 at 20:54

2 Answers2

1

Your hibernateTemplate is not being injected b/c spring does not know it needs to do anything to the class MemberDaoImpl. You need to annotate that as @Component or @Repository, just like you've done with the @Controller. As you have it now, its not a relevant target for autowiring.

zmf
  • 9,095
  • 2
  • 26
  • 28
  • I have a class which is annotated as @Service. And from that class I call MemberDAOImpl. I tried to add component in MemberDAOImpl but it gives same error. When I called service class in a plain java class by creating applicationcontext it works fine. – User123 Nov 05 '14 at 21:18
  • Give this a read. http://stackoverflow.com/questions/7414794/difference-between-contextannotation-config-vs-contextcomponent-scan – zmf Nov 05 '14 at 21:30
0

Your services and repositories must be annotated, avoid declare them through XML

First:

from

public class MemberDaoImpl implements MemberInterface{

to

@Repository
public class MemberDaoImpl implements MemberInterface{

Second

Is not recommendable use HibernateTemplate anymore, you can use directly in your repository the Hibernate SessionFactory

Third

You must annotate your @Service and @Repository with @Transactional, because Hibernate is going to ask for Transaction support. It furthermore requires define some extra beans in your configuration for the transactionManager

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158