spring和hibernate整合遇到的问题
2017-04-27 16:30
429 查看
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="acquireIncreme
4000
nt" 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>
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="acquireIncreme
4000
nt" 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>
相关文章推荐
- hibernate与spring整合所遇到的问题
- Spring整合Hibernate遇到的问题
- id to load is required for loading 对Struts,Hibernate,Spring整合开发中遇到问题
- id to load is required for loading 对Struts,Hibernate,Spring整合开发中遇到问题
- Spring 与 hibernate 整合遇到的问题
- hibernate4 和 spring3 整合注意事项:HibernateDaoSupport没有了找到篇好文章,我之前遇到的问题都在这都能找到。其实出现这些问题的关键就是hibernate4和hib
- Spring与Hibernate的整合所遇到的管理Session的问题
- flex 整合spring,hibernate遇到的一些问题
- 探讨Spring与Hibernate的整合所遇到的管理Session的问题
- struts2+hibernate+spring整合过程中遇到的问题
- (10)Spring和Hibernate整合过程中遇到的问题
- hibernate整合到spring上遇到的一个问题
- Spring 3.x 与Hibernate 4.x 整合遇到的问题
- Spring与Hibernate的整合所遇到的管理Session的问题
- Spring整合hibernate(注解)遇到unknown entity问题
- hibernate整合spring开发的时候遇到的一些小问题
- 探讨Spring与Hibernate的整合所遇到的管理Session的问题
- spring与hibernate整合的问题
- struts+spring+hibernate整合问题解决方法 陆续更新中....
- Spring2.5,Activemq5.2整合遇到的问题以及解决方法