spring-Spring整合hibernate SessionFactoryImpl的问题

问题描述

Spring整合hibernate SessionFactoryImpl的问题

各位大侠,本人在整合Spring和Hibernate4时出现了Failed to convert property value of type 'org.hibernate.internal.SessionFactoryImpl' to required type 'org.hibernate.Session' for property 'sessionFactory'问题,我的applicationContext.xml如下:

<bean id="employeeService" class="com.ssh.service.EmployeeService">
    <property name="employeeDao" ref="employeeDao"></property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.password}"></property>
    <property name="driverClass" value="${jdbc.driverClass}"></property>
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

    <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
    <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

   <!--  <context:component-scan base-package="com.ssh.*"></context:component-scan>-->
<bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    <property name="mappingLocations" value="classpath:com/ssh/entities/*.hbm.xml"></property>
</bean>

<!-- 配置xml声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true"/>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut expression="execution(* com.ssh.service.*.*(..))" id="txPointCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>

Employee.java:

public class EmployeeDao {

private SessionFactory sessionFactory;

public Session getSessionFactory() {
    return sessionFactory.getCurrentSession();
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}
@SuppressWarnings("unchecked")
public List<Employee> getAll(){
    String hql = "FROM Employee";
    return getSessionFactory().createQuery(hql).list();
}

}

EmployeeService.java:
public class EmployeeService {

private EmployeeDao employeeDao;

public EmployeeDao getEmployeeDao() {
    return employeeDao;
}

public void setEmployeeDao(EmployeeDao employeeDao) {
    this.employeeDao = employeeDao;
}

public List<Employee> getAll(){
    return employeeDao.getAll();
}

}

测试类SSHTest.java:
public class SSHTest {

private ApplicationContext ctx = null;
private EmployeeService employeeService;

{
    ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    employeeService = (EmployeeService) ctx.getBean("employeeService");
}

@Test
public void testEmployeeService() {
    System.out.println(employeeService.getAll());
}

}
异常信息如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDao' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.hibernate.internal.SessionFactoryImpl' to required type 'org.hibernate.Session' for property 'sessionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.hibernate.internal.SessionFactoryImpl] to required type [org.hibernate.Session] for property 'sessionFactory': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)

应该是applicationContext.xml中 配置的
<property name="sessionFactory" ref="sessionFactory"></property>
这一步对应的sessionFactory出错了。网上的例子这样是可以的。然后我把在xml配置bean改成用自动注入注解的方式配置结果就可以,请问这是为什么啊?

解决方案

Spring整合Hibernate问题
hibernate与spring整合所遇到的问题
关于spring整合hibernate自动创建数据表的问题

解决方案二:

http://www.cnblogs.com/hoojo/p/dynamic_switch_sessionfactory_muliteSessionFactory.html

时间: 2024-10-23 05:58:33

spring-Spring整合hibernate SessionFactoryImpl的问题的相关文章

spring mvc 整合hibernate 菜鸟级问题

问题描述 小弟想用spring mvc 整合hibernate 但是spring总是如法注入. spring配置文件如下<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">&l

基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)

1.导入jar 2.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4"  xmlns="http://java.sun.com/xml/ns/j2ee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocatio

Spring怎样整合Hibernate Search?

问题描述 Spring.xml<!--......--><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="mappingReso

spring整合hibernate出现sessionFactory的bean创建异常错误

问题描述 spring整合hibernate出现sessionFactory的bean创建异常错误 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; neste

spring 整合hibernate出错

问题描述 spring 整合hibernate出错 <?xml version ="1.0" encoding ="utf-8"?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.

SSH笔记二 整合hibernate和spring

重新整理起来才发觉,搭建一个SSH框架是件很繁琐的事情.不过总觉得自己搭建的用起来才觉得得心应手,所以尽量的从中来发现一些乐趣. 今天的任务是--整合hibernate和spring.完成的效果如下(左边为包视图.右边为文件视图): 1.准备工作(用mysql5新建数据库,暂且命名为ssimdb) 1.1 安装mysql5(略),安装完成之后可以使用Navicat8来进行管理mysql: 1.2 创建数据库 ssimdb: 1.3 创建表 ssim_user 1 drop table if ex

ssh整合-spring整合hibernate做测试时没有报错,当三个整合时启动就报错啦,求大神指点!!

问题描述 spring整合hibernate做测试时没有报错,当三个整合时启动就报错啦,求大神指点!! 报的错误org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'ecsUnsigned' on target class [class $Proxy17] failed; nested exception is org.springframework.tran

java类的问题哈哈哈哈-hibernate和spring怎么整合的

问题描述 hibernate和spring怎么整合的 hibernate和spring添加切面配置是如何搞得,请各位大神指点我,谢谢啦 解决方案 http://wenku.baidu.com/link?url=JWek_B9UHh9ZkM1l80KvA1nmq6ePWUTq94zbWQVTe_2rL89R-pWdR3y3uBM5m2aRZBcruy2k2jsniuR3CTUtbLeoGbG4UswO6qECEumiFzu 解决方案二: http://blog.163.com/cui_zhouya

java-测试Spring整合hibernate/Struts2框架时,配置文件报奇怪的错误,求解

问题描述 测试Spring整合hibernate/Struts2框架时,配置文件报奇怪的错误,求解 为什么会报无法识别property? 解决方案 怎么都是这种问题,property要包含在bean标签内 解决方案二: Struts2+hibernate+spring整合时错误Struts2+hibernate+spring整合时错误 解决方案三: http://iteye.blog.163.com/blog/static/186308096201210265334689/ 解决方案四: pro