spring和hibernate整合遇到的问题

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'?

 








org.hibernate.dialect.MySQLDialect

true

update



//这里的name改了就不报这个错了

com/sh/entiey/Dept.hbm.xml




 
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


//这里的name改了就不报这个错了

com/sh/entiey/Dept.hbm.xml



改为:
!-- 配置引入映射文件 -->


classpath:com/sh/entiey///加上classpath:,一定要去掉了Dept.hbm.xml(否则还是会出现这个错)



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的配置:

    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">
   











 








org.hibernate.dialect.MySQL57Dialect

true

update





classpath:com/sh/entiey/




 


   
   





















     


你可能感兴趣的:(环境配置)