Spring load Hibernate failed: Error creating Bean with Name ' sessionfactory ' defined in ServletContext ... Null

Source: Internet
Author: User
Tags aop postgresql

Spring load hibernate triggered a NullPointerException exception yesterday:

Org.springframework.beans.factory.BeanCreationException:Error creating Bean with Name ' sessionfactory ' defined in ServletContext resource [/web-inf/spring-config/spring-hibernate.xml]: Invocation of Init method failed; Nested exception is java.lang.NullPointerException at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean ( abstractautowirecapablebeanfactory.java:1578) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean ( abstractautowirecapablebeanfactory.java:545) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:482) at org.springframework.beans.factory.support.abstractbeanfactory$1. GetObject (abstractbeanfactory.java:305) at Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:230) at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:301) at Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (
abstractbeanfactory.java:196) .....
	caused by:java.lang.NullPointerException at Org.postgresql.jdbc.TypeInfoCache.getSQLType (typeinfocache.java:182) At Org.postgresql.jdbc.TypeInfoCache.getSQLType (typeinfocache.java:178) at Org.postgresql.jdbc.TypeInfoCache.requiresQuoting (typeinfocache.java:851) at Org.postgresql.jdbc.PgDatabaseMetaData.getTypeInfo (pgdatabasemetadata.java:2785) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native method) at Sun.reflect.NativeMethodAccessorImpl.invoke ( Unknown source) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown source) at Java.lang.reflect.Method.invoke (Unknown Source) at Org.logicalcobwebs.proxool.ProxyDatabaseMetaData.intercept (
	proxydatabasemetadata.java:60) at $java. sql.wrapper$ $EnhancerByProxool $$5b0813c3.gettypeinfo (<generated>) At Org.hibernate.engine.jdbc.spi.TypeInfo.extractTypeInfo (typeinfo.java:101) at org. Hibernate.engine.jdbc.internal.JdbcServicesImpl.configure (jdbcservicesimpl.java:163) at Org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService ( standardserviceregistryimpl.java:111) at Org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService (Abstractserviceregistryimpl.java : 234) at Org.hibernate.service.internal.AbstractServiceRegistryImpl.getService (Abstractserviceregistryimpl.java : 206) at Org.hibernate.cfg.Configuration.buildTypeRegistrations (configuration.java:1885) at Org.hibernate.cfg.Configuration.buildSessionFactory (configuration.java:1843) at Org.hibernate.cfg.Configuration.buildSessionFactory (configuration.java:1928) at Org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory ( localsessionfactorybuilder.java:372) at Org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory (Localsessionfactorybean.java : 454) at Org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropeRtiesset (localsessionfactorybean.java:439) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods ( abstractautowirecapablebeanfactory.java:1637) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean ( abstractautowirecapablebeanfactory.java:1574) ... More

Feeling rather depressed, because not the first day with Ah, this spring+hibernate configuration has been no problem, and then spent two hours constantly checking the mother and Google, not fruit. The heart has unwilling, opens the spring-hibernate.xml to look carefully:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
	Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"
	Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/conte XT Http://www.springframework.org/schema/context/spring-context-4.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/S CHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd Http://www.springframework . ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/s CHEMA/AOP http://www.springframework.org/schema/aop/spring-Aop-4.1.xsd "> <!--Configure hibernate session factory--> <bean id=" sessionfactory "class=" Org.springfram Ework.orm.hibernate4.LocalSessionFactoryBean "destroy-method=" destroy "> <!--<bean id=" Sessionfactory "CLA ss= "Com.freestyle.common.spring.MyLocalSessionFactoryBean" destroy-method= "destroy" >--> <property name= "DataSource" ref= "DataSource" ></property> <property name= "Packagestoscan" > <!--Hibernate Sessionfactory managed class storage location, automatic load--> <list> <value>com.alco.bms.entities</value> </li st> </property> <property name= "hibernateproperties" > <props> <prop key= "Hibernat E.dialect >org.hibernate.dialect.PostgreSQLDialect</prop> <!--settings Auto Create | update | Validate database table structure--> <prop key= "Hibernate.hbm2ddl.auto" >update</prop> <!--whether to display SQL--> <prop key= "Hibernate.show_sql" in the console
				
          >true</prop>      <!--format SQL, optimize display--> <prop key= "Hibernate.format_sql" >true</prop&
                
                
                Gt
                <!--open Level two cache--> <prop key= "Hibernate.cache.use_second_level_cache" >false</prop>
                <!--whether to turn on query caching--> <prop key= "Hibernate.cache.use_query_cache" >false</prop>
                <!--database Batch query maximum number--> <prop key= "Hibernate.jdbc.fetch_size" >100</prop> <!--database batch update, add, delete operation maximum number--> <prop key= "Hibernate.jdbc.batch_size" >50 </prop> <!--autocommit transaction--> <prop key= "Hibernate.connection.autocommit" >fa lse</prop> <!--Specify when hibernate will release the JDBC connection--> <prop key= "hibernate.connection. Release_mode ">auto</prop> <prop key=" Hibernate.enable_lazy_load_No_trans ">false</prop> <!--<prop key=" Hibernate.use_sql_comments ">true 
			</prop>--> <prop key= "Log4j.logger.org.hibernate.type" >TRACE</prop> </props> </property> </bean> <!--Configure Transaction exception encapsulation--> <bean id= "Persistenceexceptiontra Nslationpostprocessor "class=" Org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor "/ > <!--define transaction management--> <bean id= "TransactionManager class=" Org.springframework.orm.hibernate4.Hiberna Tetransactionmanager "> <property name=" sessionfactory "ref=" Sessionfactory "/> </bean> < Tx:annotation-driven transaction-manager= "TransactionManager"/> <aop:aspectj-autoproxy proxy-target-class= " True "/> <context:component-scan base-package=" Com.alco.bms.dao/> <!--definition autowired from 
 Dynamic Injection Bean--> <!--   <bean class= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>--> &l T;/beans>

Configuring Localsessionfactorybean in spring also sets 3 properties, and nullpointerexception should be caused by one of these three null values. So I wrote a Shell Mylocalsessionfactorybean is extends Localsessionfactorybean, I am in the new class to debug a look at the entry is not NULL value:

package com.freestyle.common.spring;

Import java.util.Properties;

Import Javax.sql.DataSource;

Import Org.springframework.orm.hibernate4.LocalSessionFactoryBean;



Import Com.freestyle.utils.Util; public class Mylocalsessionfactorybean extends Localsessionfactorybean {public void Setdatasource (DataSource
		DataSource) {if (datasource==null) {util.sleep (100);
		} System.out.println (DataSource);		
	Super.setdatasource (DataSource);
		public void Setpackagestoscan (String ... packagestoscan) {if (packagestoscan==null) {util.sleep (100);
		} System.out.println (Packagestoscan);
	Super.setpackagestoscan (Packagestoscan); } public void Sethibernateproperties (Properties hibernateproperties) {if (hibernateproperties==null) {util.sleep (100
		);
		} System.out.println (Hibernateproperties);
	Super.sethibernateproperties (hibernateproperties);
		
		Public Mylocalsessionfactorybean () {super (); TODO auto-generated Constructor stub} 
Will. Sleep Place all breakpoint, will spring-hibernate.xml inside Sessionfactory class replaced Mylocalsessionfactorybean again debugging, found that the parameters are not null Ah, this is silly.

Later, looking at the details of exception, I found this line: at Org.postgresql.jdbc.TypeInfoCache.getSQLType (typeinfocache.java:182), Description may be the database or JDBC driver exception, should not have spring/hibernate, so the proxool.xml data source switch to another server (two server database structure) and then run, incredibly nothing ...

This is a bit puzzled, finally no recruit, only suspected JDBC driver, because with the PostgreSQL database, the server is PostgreSQL 9.4 64bit, JDBC driver used is Postgresql-9.4.1208.jre6.jar, later on PostgreSQL official website under the Postgresql-9.4.1209.jar change. All OK!!!

Don't believe it, then change back to version number 1208 jar, out of exception, for 1209 jar, ok!!

Originally you are such PostgreSQL drive ah ...


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.