Results 1 to 3 of 3

Thread: Issue in session factory -----Spring MVC + hibernate

  1. #1
    Join Date
    Jan 2012
    Location
    Chennai
    Posts
    2

    Default Issue in session factory -----Spring MVC + hibernate

    Hi guys ,
    I'm developing a project in Spring MVC + hibernate technology.
    My issue is , could not autowire the sessionfactory field.
    if i autowire, i'm facing an issue is that,

    " org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)} ".


    Meanwhile i'm getting an java.lang.NullPointer Exception for this code
    sessionFactory.getCurrentSession().save(form);

    can anyone help me in this ?.






    package com.epro.DAO;

    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autow ired;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transac tional;

    import com.epro.controller.FormController;
    import com.epro.model.Userregistration;

    @Repository
    @Transactional
    public class RegisterDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public String saveRegistration(Userregistration form) {
    try {
    System.out.println("Saving to DB");
    sessionFactory.getCurrentSession().save(form);
    } catch (Exception e) {
    System.out.println("Exception>>>>>"+e);
    e.printStackTrace();
    }
    return "registerForm";

    }

    }

  2. #2
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    287

    Default

    Hi,

    Can you post your configurations?
    Amila Domingo

  3. #3
    Join Date
    Jan 2012
    Location
    Chennai
    Posts
    2

    Default

    web.xml file

    <?xml version="1.0" encoding="UTF-8" ?>
    - <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
    <display-name>Email</display-name>
    - <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    - <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
    - <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>
    - <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/application-context.xml</param-value>
    </context-param>
    </servlet-mapping>
    - <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>
    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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    - <!-- Enable annotation driven controllers, validation etc...
    -->
    <mvc:annotation-driven />
    <context:component-scan base-package="com.epro" />
    - <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    - <property name="prefix">
    <value>/WEB-INF/views/</value>
    </property>
    - <property name="suffix">
    <value>.jsp</value>
    </property>
    </bean>
    </beans>
    application-context.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <context:annotation-config />
    <mvc:annotation-driven />
    <import resource="webmvc-context.xml" />
    <import resource="datasource-context.xml" />
    </beans>
    webmvc-context.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <contextroperty-placeholder location="classpath:database.properties" />
    <bean class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter" />
    </beans>
    datasource-context.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    - <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="${hibernate.connection.driver_class}" />
    <property name="url" value="${hibernate.connection.url}" />
    <property name="username" value="${hibernate.connection.username}" />
    <property name="password" value="${hibernate.connection.password}" />
    </bean>
    - <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    - <property name="hibernateProperties">
    - <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    </props>
    </property>
    - <property name="mappingResources">
    - <list>
    <value>com/epro/model/Sendsms.hbm.xml</value>
    <value>com/epro/model/Userregistration.hbm.xml</value>
    </list>
    </property>
    </bean>
    - <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    </beans>
    database.properties

    # Hibernate properties
    hibernate.connection.driver_class = net.sourceforge.jtds.jdbc.Driver
    hibernate.connection.url = jdbc:jtds:sqlserver://127.0.0.1:1433/Gmail_Application_Apache
    hibernate.connection.username = sa
    hibernate.connection.password = root
    hibernate.dialect=org.hibernate.dialect.SQLServerD ialect
    hibernate.show_sql=true
    This is the exception in DAO layer i'm getting.

    Exception>>>>>java.lang.NullPointerException
    java.lang.NullPointerException
    at com.epro.DAO.RegisterDAO.saveRegistration(Register DAO.java:27)
    at com.epro.service.UserRegistrationImpl.addUsers(Use rRegistrationImpl.java:19)
    at com.epro.controller.FormController.RegisterForm(Fo rmController.java:107)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.invokeHandlerMethod(HandlerMeth odInvoker.java:175)
    at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:421)
    at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:409)
    at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:774)
    at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
    Last edited by saravana; May 11th, 2012 at 11:39 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •