spring和hibernate整合遇到的问题

来源:互联网 发布:福彩3d软件分析软件 编辑:程序博客网 时间:2024/05/21 22:43
1.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [bean.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'mappingResource' of bean class [org.springframework.orm.hibernate5.LocalSessionFactoryBean]: Bean property 'mappingResource' is not writable or has an invalid setter method. Did you mean 'mappingResources'?

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'mappingResource' of bean class [org.springframework.orm.hibernate5.LocalSessionFactoryBean]: Bean property 'mappingResource' is not writable or has an invalid setter method. Did you mean 'mappingResources'?

<!-- #############spring和hibernate整合  start########### -->  
<!-- 配置hibernate中的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池对象 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate的相关属性 -->
<property name="hibernateProperties">
<props>
<!-- 配置Hibernate的方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 显示sql -->
<prop key="hibernate.show_sql">true</prop>
<!-- 格式化DDL的自动创建 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 配置引入映射文件 -->
<property name="mappingDirectoryLocations">//这里的name改了就不报这个错了
<list>
<value>com/sh/entiey/Dept.hbm.xml</value>
</list>

</property>
</bean>
<!-- #############spring和hibernate整合  end########### -->  
2.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [bean.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Mapping directory location [class path resource [com/sh/entiey/Dept.hbm.xml]] does not denote a directory

Caused by: java.lang.IllegalArgumentException: Mapping directory location [class path resource [com/sh/entiey/Dept.hbm.xml]] does not denote a directory

<!-- 配置引入映射文件 -->
<property name="mappingDirectoryLocations">//这里的name改了就不报这个错了
<list>
<value>com/sh/entiey/Dept.hbm.xml</value>
</list>

</property>
改为:
!-- 配置引入映射文件 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/sh/entiey/</value>//加上classpath:,一定要去掉了Dept.hbm.xml(否则还是会出现这个错)
</list>

</property>
3.
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.sh.service.DeptDervice] for bean with name 'deptService' defined in class path resource [bean.xml]; nested exception is java.lang.ClassNotFoundException: com.sh.service.DeptDervice

Caused by: java.lang.ClassNotFoundException: com.sh.service.DeptDervice

是配置中我的service改了名称,配置中没改
4.
org.hibernate.exception.SQLGrammarException: could not execute statement

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'sh.dept' doesn't exist

可能是因为在项目中添加的数据库驱动包与Hibernate.xml文件中所使用的方言版本不同引起的;
解决方案:如果使用的数据库的驱动包为mysql-connector-java-5.1.5-bin.jar,
则方言使用:org.hibernate.dialect.MySQL5Dialect
我的mysql是5.7所以用org.hibernate.dialect.MySQL57Dialect也可以
不能用org.hibernate.dialect.MySQLDialect




运行成功的spring的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
   
<!-- 数据源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///sh"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<!-- #############spring和hibernate整合  start########### -->  
<!-- 配置hibernate中的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池对象 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate的相关属性 -->
<property name="hibernateProperties">
<props>
<!-- 配置Hibernate的方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</prop>
<!-- 显示sql -->
<prop key="hibernate.show_sql">true</prop>
<!-- 格式化DDL的自动创建 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 配置引入映射文件 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/sh/entiey/</value>
</list>

</property>
</bean>
<!-- #############spring和hibernate整合  end########### -->  
<!-- 实例 -->
<bean id="deptDao" class="com.sh.dao.DeptDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
<bean id="deptService" class="com.sh.service.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>
<!-- 事务配置 -->
<!-- a.配置事务管理器类 -->
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- b.配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- c.AOP配置 -->
<aop:config>
<aop:pointcut expression="execution(* com.sh.service.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>

</beans>      


0 0
原创粉丝点击