我已经尝试使用 Spring Data 存储库并尝试设置一个简单的存储库已有 2 天了。
我有一个使用 CrudRepository 的简单 UserRepository
我有我的实体类
这是我的 XML,其中突出显示的行在网络浏览器中给出错误
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:context="http://www.springframework.org/schema/context" xmlns:repository="http://www.springframework.org/schema/data/repository" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/test"/> <property name="username" value="root"/> <property name="password" value=""/> </bean> <jpa:repositories base-package="com.mydata"/> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true"/> <property name="generateDdl" value="true"/> <property name="database" value="test"/> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/> <!-- spring based scanning for entity classes--> <property name="packagesToScan" value="com.mydata"/> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/> </beans>
当我访问初始化此 beans 类的 Controller 时,我在网络浏览器中收到以下错误
Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.orm.jpa.vendor.Database' for property 'database'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.orm.jpa.vendor.Database] for property 'database': no matching editors or conversion strategy found
出现此错误是由于 XML 中的行 property name="database"value="test"
沙赫扎德
最佳答案
JavaDoc中所示database
属性采用数据库类型,而不是名称。
一般来说,我建议更多地关注 Spring Boot 及其 JPA 支持(在此处查找 a guide),因为它可以帮助您摆脱所有样板配置设置。
关于java - Spring 数据存储库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37840896/