java - Spring 数据存储库的问题

标签 java spring repository

我已经尝试使用 Spring Data 存储库并尝试设置一个简单的存储库已有 2 天了。

  1. 我有一个使用 CrudRepository 的简单 UserRepository

  2. 我有我的实体类

  3. 这是我的 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/

相关文章:

java - 与替代 Java 的组合

java - 我如何使用java获得多个单词的上位词

java - 在 Spring Boot 上删除 "Using default security password"

git - 与 Visual Studio 存储库中的共享库

ubuntu - 从本地 repo 中删除 deb-packages 并配置 apt

java - 检查 ArrayList 中存储的单词出现的次数

java - 为什么 Hibernate 在使用 GenerationType.IDENTITY 策略时尝试访问 hibernate_sequence?

java - 从工作站中删除 Eclipse。我如何告诉 SVN 忽略文件并填充版本标签

spring boot feign客户端获取HTTP 404

java - Gradle:配置运行时声明对配置默认值的依赖,该依赖项未在模块描述符中声明