救命啊,spring获取hibernate sessionfactory 失败啊。
配置:spring 2.1,hibernate 3.2
我用spring的配置文件想获取一个hibernatedaosupport类型的bean,但是总是提示nullpointerexception,程序跟踪后发现hibernatetemplate 竟然是null的。然后我想用spring获取LocalSessionFactoryBean,又提示转换失败,我用spring获取其他的bean是正常,这到底是什么回事。
spring配置如下:
<beans>
<bean id= "SybDataSource "
class= "org.apache.commons.dbcp.BasicDataSource " destroy-method= "close ">
<property name= "driverClassName ">
<value> com.sybase.jdbc3.jdbc.SybDataSource </value>
</property>
<property name= "url ">
<value> jdbc:sybase:Tds:132.119.32.102:5000/ss7info?charset=eucgb&JCONNECT_VERSION=0
</value>
</property>
<property name= "username ">
<value> sa </value>
</property>
<property name= "password ">
<value> *** </value>
</property>
<property name= "maxActive "> <value> 8 </value> </property>
<property name= "maxIdle "> <value> 4 </value> </property>
<property name= "maxWait "> <value> 30 </value> </property>
</bean>
<bean id= "HibSessionFactory "
class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean ">
<property name= "dataSource ">
<ref bean= "SybDataSource " />
</property>
<property name= "hibernateProperties ">
<props>
<prop key= "hibernate.dialect "> org.hibernate.dialect.SybaseDialect </prop>
<prop key= "showSQL "> false </prop>
</props>
</property>
</bean>
<bean id= "BasicDAO " class= "com.test.ss7info.dao.BasicDAO ">
<property name= "sessionFactory "> <ref local= "HibSessionFactory "> </ref> </property>
</bean>
</beans>
------解决方案--------------------可能是包没有导完全的原因,导致dataSource没有初始化成功,是否导入了spring中有关dbcp相关的第三方的jar包,我记得应该是有一个pool的包.
------解决方案--------------------谢谢ls的关注。但是dbcp和driver都已经导入了,而且我试过获取datasource都是可用的,就是不知道为什么hibernatetemplate 是空的。
------解决方案--------------------你换一种用法试试,把配置信息写到hibernate.cfg.xml中。代码似乎没问题啊!
<bean id= "sessionFactory " class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean ">
<property name= "configLocation ">
<value> classpath:/com/.../hibernate.cfg.xml </value>
</property>
</bean>
------解决方案--------------------41801837 程序员技术交流群 (如果需求太高,再好的程序也会变得糟糕!)
------解决方案--------------------你的实体配置文件怎么没有?XXX.hbm.xml
<bean id= "sessionFactory "
class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean ">
<property name= "dataSource ">
<ref bean= "dataSource " />
</property>
<property name= "hibernateProperties ">
<props>