Spring Data Repositories的问题

时间:2016-06-15 16:27:31

标签: java spring repository

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

  1. 我使用CrudRepository

  2. 拥有简单的UserRepository
  3. 我有我的实体类

  4. 这是我的XML,突出显示的行在Web浏览器中给出了错误

    <?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>   
    
  5. 当我访问我在其中启动此bean类的控制器时,我在Web浏览器中出现以下错误

    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中的行属性名称=“数据库”值=“测试”

    而发生此错误

    沙赫扎德

2 个答案:

答案 0 :(得分:1)

JavaDoc中指示database属性采用数据库类型,而不是名称。

一般情况下,我建议更多地考虑一下Spring Boot及其JPA支持(在此处查找a guide),因为它可以帮助您完成所有样板配置设置。

答案 1 :(得分:0)

您是否尝试过MYSQL数据库值?我认为Spring应该将其隐式转换为Database.MYSQL

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true"/>
    <property name="generateDdl" value="true"/>
    <property name="database" value="MYSQL"/>
</bean>

或Spring Boot bean定义

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
    hibernateJpaVendorAdapter.setShowSql(true);
    hibernateJpaVendorAdapter.setGenerateDdl(true);
    hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
    return hibernateJpaVendorAdapter;
}