IceStormSpirit 2015-09-01 11:19 采纳率: 0%
浏览 3741

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改成用自动注入注解的方式配置结果就可以,请问这是为什么啊?
  • 写回答

1条回答

  • devmiao 2015-09-01 14:58
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能