java - 我的坚持有什么问题吗?

标签 java hibernate spring-mvc

我使用 springmvc 来构建我的项目。但我有一个问题:

持久性.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="defaultPersistenceUnit"  transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="hibernate.connection.url"
                      value="jdbc:mysql://localhost:3306/springdemo"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="hukangze"/>
            <property name="hibernate.show_sql" value="true"/>

            <property name="hibernate.connection.useUnicode" value="true"/>
            <property name="hibernate.connection.characterEncoding" value="UTF-8"/>

            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.use_sql_comments" value="false"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>

            <property name="hibernate.connection.autoReconnect" value="true"/>
            <property name="connection.autoReconnectForPools" value="true"/>
            <property name="connection.is-connection-validation-required" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

mvc-dispatcher-servlet.xml:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.ssh.controller"/>

    <mvc:default-servlet-handler/>

    <mvc:annotation-driven/>

    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <jpa:repositories base-package="com.ssh.repository"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="defaultPersistenceUnit"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" value="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

当我运行它时,我收到此错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'javax.persistence.EntityManagerFactory' for property 'entityManagerFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'javax.persistence.EntityManagerFactory' for property 'entityManagerFactory': no matching editors or conversion strategy found
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'javax.persistence.EntityManagerFactory' for property 'entityManagerFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'javax.persistence.EntityManagerFactory' for property 'entityManagerFactory': no matching editors or conversion strategy found
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'javax.persistence.EntityManagerFactory' for property 'entityManagerFactory': no matching editors or conversion strategy found
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'javax.persistence.EntityManagerFactory' for property 'entityManagerFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'javax.persistence.EntityManagerFactory' for property 'entityManagerFactory': no matching editors or conversion strategy found

但是当我在 mvc-dispatcher-servlet.xml 中撤消此代码时:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" value="entityManagerFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

没关系。会发生什么?这段代码的作用是什么?

最佳答案

您完整阅读了异常吗?非常详细:

...property value of type 'java.lang.String' to required type 'javax.persistence.EntityManagerFactory' for property 'entityManagerFactory'...

更改此行:

<property name="entityManagerFactory" value="entityManagerFactory"/>

对此:

<property name="entityManagerFactory" ref="entityManagerFactory"/>

关于java - 我的坚持有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42183575/

相关文章:

java - 为什么不使用 @ControllerAdvice 中的通用异常处理程序?

java - 正确使用Java异常

java - Web 服务和用户的 Spring 安全性

java - .putExtra Intent 后应用程序崩溃

hibernate - 延迟加载一个类的 Blob 属性

database - 如何避免 'Could not determine Hibernate dialect for database name [H2]!' ?

java - 根据值对 map 进行排序的通用方法

java - 无法将正常编码文本放入数据库表 : Hibernate 4. 3.4 + mySQL 5.1.30

Spring mvc : Changing default Response format from xml to json

java - Spring 中使用 HibernateTemplate 的 ModelMapper 的奇怪行为