Announcement Announcement Module
Collapse
No announcement yet.
Error in spring-hibernate application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error in spring-hibernate application

    I am new to Spring, i am trying to create a simple project with spring and Hibernate with annotations(without use of configuration and mapping XML's). I am held up with this below error since 3 days.

    Below is the stack trace of the error
    Code:
    ERROR [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-1) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:591) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) [org.springframework.context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469) [org.springframework.context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    	at javax.servlet.GenericServlet.init(GenericServlet.java:242) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202) [jbossweb-7.0.13.Final.jar:]
    	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1102) [jbossweb-7.0.13.Final.jar:]
    	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3655) [jbossweb-7.0.13.Final.jar:]
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:3873) [jbossweb-7.0.13.Final.jar:]
    	at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    	at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_25]
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_25]
    	at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_25]
    Caused by: java.lang.NullPointerException
    	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:207) [hibernate-core-4.1.3.Final.jar:4.1.3.Final]
    	at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) [hibernate-core-4.1.3.Final.jar:4.1.3.Final]
    	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) [hibernate-core-4.1.3.Final.jar:4.1.3.Final]
    	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) [hibernate-core-4.1.3.Final.jar:4.1.3.Final]
    	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71) [hibernate-core-4.1.3.Final.jar:4.1.3.Final]
    	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2277) [hibernate-core-4.1.3.Final.jar:4.1.3.Final]
    	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2273) [hibernate-core-4.1.3.Final.jar:4.1.3.Final]
    	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742) [hibernate-core-4.1.3.Final.jar:4.1.3.Final]
    	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782) [hibernate-core-4.1.3.Final.jar:4.1.3.Final]
    	at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242) [org.springframework.orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]

    Here is the code

    [ Spring-servlet.xml]
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans ...>
    
    	<context:property-placeholder location="classpath:jdbc.properties" />
    	
    	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    		<property name="prefix" value="/WEB-INF/jsp/" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${database.driver}" />
    		<property name="url" value="${database.url}" />
    		<property name="username" value="${database.username}" />
    		<property name="password" value="${database.password}" />
    	</bean>
    
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="annotatedClasses">
    			<list>
    				<value>com.spring.form.Items</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
    				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    				<prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    	
        <context:component-scan base-package="com.spring" />
        
        <tx:annotation-driven transaction-manager="transactionManager"/>
        
        
    </beans>
    Controller
    Code:
    @Controller
    @RequestMapping("/receiptsnew")
    public class ReceiptsSpringController {
    
    @Autowired
    private ReceiptsService receiptsService;
    
    public ReceiptsSpringController()
    {
    
    }
    
    @RequestMapping(value="/save", method=RequestMethod.POST)
    public ModelAndView saveItems(@ModelAttribute("item")Items item, BindingResult result)
    {
        receiptsService.addItem(item);
    
        return new ModelAndView("redirect:/receiptsnew.html");
    
    }
    
    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView viewItems()
    {
        Map model= new HashMap();
        model.put("items", receiptsService.getAllItems());
        //items is just a key values
    
        return new ModelAndView("itemsView",model);
    }
    
    @RequestMapping(value="/add", method=RequestMethod.GET)
    public ModelAndView addItems(@ModelAttribute("item")Items item, BindingResult result) 
    {
        return new ModelAndView("itemAdd"); 
    }
    }
    DAO

    Code:
    import java.util.List;
    
    
    @Repository("itemDao")
    public class HibernateItemDao implements ItemDao {
    
    @Autowired
    private SessionFactory sessionFactory;
    
    
    public void saveItem(Items item) {
        // TODO Auto-generated method stub
        sessionFactory.getCurrentSession().saveOrUpdate(item);
    }
    
    @SuppressWarnings("unchecked")
    public List<Items> getAllItems() {
        // TODO Auto-generated method stub
        return sessionFactory.getCurrentSession().createCriteria(Items.class).list();
    }
    
    }
    Service layer
    Code:
    @Service("receiptsService")
    @Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
    public class ReceiptsServiceImpl implements ReceiptsService {
    
    @Autowired
    private ItemDao itemDao;
    
    @Transactional(propagation=Propagation.REQUIRED,readOnly=false)
    public void addItem(Items item) {
        // TODO Auto-generated method stub
        itemDao.saveItem(item);
    }
    
    public List<Items> getAllItems() {
        // TODO Auto-generated method stub
        return itemDao.getAllItems();
    }
    
    }

    jar's

    Attachment

    properties

    Attachment
    Attached Files
    Last edited by ucancprem; Nov 9th, 2012, 09:03 PM.

  • #2
    how do i know if some one is looking at this??

    Comment


    • #3
      I'm also new to this stuff but the error indicates that you've got an error/issue in this code :

      Code:
      	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      		<property name="dataSource" ref="dataSource" />
      		<property name="annotatedClasses">
      			<list>
      				<value>com.spring.form.Items</value>
      			</list>
      		</property>
      		<property name="hibernateProperties">
      			<props>
      				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
      				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      				<prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>
      			</props>
      		</property>
      	</bean>
      Are you sure the ${hibernate.dialect} and other macros are being filled in with real values? I'd try hard-coding them to particular values and see if your issue(s) go away..

      I know the java error messages are a pain to weed through but the key items of interest are the following :

      Code:
      ERROR [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-1) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
      It's telling you that it tried to create/instantiate a sessionFactory bean based on the info you provided above but had an issue with it for whatever reason.. That should be enough to dig deeper into that set of properties to play around.. and hopefully determine what is causing your problem..

      Comment


      • #4
        Thank you!!!, i shall try that and will post the outcome...

        Comment


        • #5
          That'd be nice.. I think in general you should always look at that very first line of the exception to give you a good idea of what crapped out -- I believe the rest is just a stack-dump of how it got there.. In your particular case it may be that the code is missing (or whatever fills that stuff in) that initializes those macros to particular values -- once you figure that out you're probably ready to continue..

          Comment


          • #6
            Unfortunately, that one did not solve the error... still get the same error...

            org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException....

            Comment


            • #7
              Can you post the full content of your spring-servlet.xml file?

              Comment


              • #8
                <?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:tx="http://www.springframework.org/schema/tx"
                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/tx
                http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

                <contextroperty-placeholder location="classpath:jdbc.properties" />

                <bean id="jspViewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlVi ew" />
                <property name="prefix" value="/WEB-INF/jsp/" />
                <property name="suffix" value=".jsp" />
                </bean>

                <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
                <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@app1.coreebusiness.com:15 21:oracle" />
                <property name="username" value="Allegro" />
                <property name="password" value="Allegro" />
                </bean>

                <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSes sionFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="annotatedClasses">
                <list>
                <value>com.spring.form.Items</value>
                </list>
                </property>
                <property name="hibernateProperties">
                <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Orac le10gDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.connection.pool_size">20</prop>
                </props>
                </property>
                </bean>

                <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTran sactionManager">
                <property name="sessionFactory" ref="sessionFactory" />
                </bean>

                <context:component-scan base-package="com.spring" />

                <tx:annotation-driven transaction-manager="transactionManager"/>


                </beans>

                Comment


                • #9
                  Can you please re-post or edit the above post to put the code snippet in [code] tags please? That way nothing is misinterpretted by the forum software.. like it is now with occasional happy faces, added spaces, etc..

                  Comment


                  • #10
                    Code:
                    <?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:tx="http://www.springframework.org/schema/tx"
                    	xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
                    
                    	<context:property-placeholder location="classpath:jdbc.properties" />
                    	
                    	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                    		<property name="prefix" value="/WEB-INF/jsp/" />
                    		<property name="suffix" value=".jsp" />
                    	</bean>
                    
                    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                    		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
                    		<property name="url" value="jdbc:oracle:thin:@app1.coreebusiness.com:1521:oracle" />
                    		<property name="username" value="Allegro" />
                    		<property name="password" value="Allegro" />
                    	</bean>
                    
                    	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                    		<property name="dataSource" ref="dataSource" />
                    		<property name="annotatedClasses">
                    			<list>
                    				<value>com.spring.form.Items</value>
                    			</list>
                    		</property>
                    		<property name="hibernateProperties">
                    			<props>
                    				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                    				<prop key="hibernate.show_sql">false</prop>
                    				<prop key="hibernate.connection.pool_size">20</prop>
                    			</props>
                    		</property>
                    	</bean>
                    
                    	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
                    		<property name="sessionFactory" ref="sessionFactory" />
                    	</bean>
                    	
                        <context:component-scan base-package="com.spring" />
                        
                        <tx:annotation-driven transaction-manager="transactionManager"/>
                    </beans>

                    Comment


                    • #11
                      Do you have a pom.xml you can show (in code-tags too please)? Do you have the jta.jar in there? I found a reference to that missing in another post

                      Comment


                      • #12
                        I do not have a pom.xml... I tried placing the jta.jar file in my WEB-INF/lib folder... but i still continue to have the same problem...

                        here is the list of jar's i have in the lib ..Attachment
                        Attached Files

                        Comment


                        • #13
                          sorry.. pom.xml is specific to Maven.. My bad.. I'd suggest at this point making sure all of the requisite libraries are in place and they're up-to-date (not mixing-n-matching old + new,etc).. Unfortunately I'm out of ideas..

                          P.S. I pulled up the pic but it is so small on my side that I can't make anything out -- even with magnifying it..

                          Comment


                          • #14
                            is there any way i can send u that image of the jar files??

                            Comment


                            • #15
                              you could pm it to me I suspect..

                              Comment

                              Working...
                              X