hibernate+spring整合过程sessionFactory一直空指针

馨馨勇敢走 2014-07-17 12:55:08
整合过程都是按照文档上修改复制的,其他注入都正常,唯独sessionFactory一直为空,感觉像是xml中的配置有问题,实在找不出来了,请大神帮忙瞅瞅。

XML代码:

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=UserInfo"/>
<property name="username" value="sa"/>
<property name="password" value="0"/>
</bean>

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>cn/test/entity/UserMessage.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.connection.pool_size=1
hibernate.hbm2ddl.auto=update
</value>
</property>
</bean>

sessionFactory注入配置还有获取代码:


<bean id="baseDao" class="cn.test.dao.BaseDao">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
********************************************************************************
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

protected Session getSession() {
return this.getSessionFactory().getCurrentSession();
}

public SessionFactory getSessionFactory() {
return sessionFactory;
}

...全文
852 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你前面已经注入sessionFactory了,不要用set方法 直接注释Autowired去获取sessionFactory试一下
@Autowired
private SessionFactory sessionFactory;
qq_25127781 2016-04-11
  • 打赏
  • 举报
回复
你们14年的问题,我16年遇到了,我是不是跟不上你们的步伐了?
skyhitnow 2015-01-08
  • 打赏
  • 举报
回复
<property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.SQLServerDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.connection.pool_size=1 hibernate.hbm2ddl.auto=update </value> </property> 这种写法对吗? 应该是这样吧 <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.current_session_context_class">thread</prop> </props> </property>
pl在之心 2015-01-07
  • 打赏
  • 举报
回复
请问楼主解决了没,我也出现这样的问题了,坑啊
放纵的青春 2014-07-18
  • 打赏
  • 举报
回复
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDao' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [cn.test.dao.BaseDao]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 仔细检查一下这块的配置呢
馨馨勇敢走 2014-07-17
  • 打赏
  • 举报
回复
馨馨勇敢走 2014-07-17
  • 打赏
  • 举报
回复
来个猛牛吧
wen772589 2014-07-17
  • 打赏
  • 举报
回复
private SessionFactory sessionFactory; 是把这个名称换下看
wen772589 2014-07-17
  • 打赏
  • 举报
回复
把mySessionFactory名称全部改成小写!
zishinan 2014-07-17
  • 打赏
  • 举报
回复
检查basedao
馨馨勇敢走 2014-07-17
  • 打赏
  • 举报
回复
引用 13 楼 rui888 的回复:
又可 能是jar 版本,或者jar 冲突的问题。




hibernate是3.6.8的,spring是3.1.1的,jar包应该不会有问题,其他的注入都能完成,就是sessionFactory获取不到,

tony4geek 2014-07-17
  • 打赏
  • 举报
回复
又可 能是jar 版本,或者jar 冲突的问题。
馨馨勇敢走 2014-07-17
  • 打赏
  • 举报
回复
引用 11 楼 rui888 的回复:
那再改回来。
这个问题奇葩了,我写了个hibernate的单独测试,能成功,感觉应该配置上木有什么大问题了,郁闷了半天了,咋还不出来。。。。。。。。。。。。。。。
tony4geek 2014-07-17
  • 打赏
  • 举报
回复
那再改回来。
馨馨勇敢走 2014-07-17
  • 打赏
  • 举报
回复
引用 9 楼 rui888 的回复:
估计名称也错了参考这个。
private SessionFactory mySessionFactory;这个mySessionFactory在bean中的<property name="sessionFactory" ref="mySessionFactory"/>name是sessionFactory,注入的是sessionFactory,私有化的mySessionFactory,肯定会出问题
tony4geek 2014-07-17
  • 打赏
  • 举报
回复
馨馨勇敢走 2014-07-17
  • 打赏
  • 举报
回复
引用 7 楼 rui888 的回复:
Caused by: 后面的错误呢/。?
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDao' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [cn.test.dao.BaseDao]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1396) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at cn.test.test.SHtest.setUpBeforeClass(SHtest.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [cn.test.dao.BaseDao]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064) at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:924) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393) ... 28 more
tony4geek 2014-07-17
  • 打赏
  • 举报
回复
Caused by: 后面的错误呢/。?
馨馨勇敢走 2014-07-17
  • 打赏
  • 举报
回复
引用 5 楼 rui888 的回复:
private SessionFactory mySessionFactory; 然后 加 get/set
信息: Building new Hibernate SessionFactory 2014-7-17 13:28:23 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons 信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@b8deef: defining beans [myDataSource,mySessionFactory,userDao,baseDao]; root of factory hierarchy 2014-7-17 13:28:23 org.springframework.orm.hibernate3.AbstractSessionFactoryBean destroy 信息: Closing Hibernate SessionFactory 数据也插入不进去
tony4geek 2014-07-17
  • 打赏
  • 举报
回复
private SessionFactory mySessionFactory; 然后 加 get/set
加载更多回复(2)

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧