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>
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
- spring和hibernate整合遇到的问题
- Spring 与 hibernate 整合遇到的问题
- Spring整合Hibernate遇到的问题
- flex 整合spring,hibernate遇到的一些问题
- hibernate与spring整合所遇到的问题
- struts2+hibernate+spring整合过程中遇到的问题
- spring整合hibernate中遇到的坑
- spring ,springMVC,Mybatis和ehcache整合遇到的问题
- hibernate和spring的整合
- spring 和hibernate的整合
- hibernate和spring的整合
- Spring和Hibernate的整合
- Hibernate和Spring的整合
- spring和hibernate的整合
- Hibernate和Spring的整合
- Spring和Hibernate的整合
- Spring和Hibernate的整合
- struts2与hibernate整合遇到的问题
- 爬取新浪微博数据+新浪微博模拟登录+mysql+python
- mysql中sql语句的妙用
- kMeans算法(K均值聚类算法)
- servlet
- nec css规范
- spring和hibernate整合遇到的问题
- 无人与我立黄昏,无人问我粥可温。
- Android开发-高级网络组件(2)使用Volley加载网络图片-AndroidStudio
- 2017年4月26日 阿里巴巴笔试编程题 股票
- 利用SSL For Free工具3分钟获取Let's Encrypt免费SSL证书(https免费证书)
- 查看linux操作系统版本号,查看glibc版本号
- jsp笔记
- linux 出现大量TIME_WAIT
- Git命令大全及相关概念