4

我正在尝试将自定义行为添加到基本的 Spring Data JPA 存储库,并已根据此处列出的文档http://docs.spring.io/spring-data/jpa/docs/1.2.0.RELEASE/reference/ html/#repositories.single-repository-behaviour

但是,似乎框架没有将方法识别为客户方法,而是尝试解析方法的名称并创建查询。我收到此错误:org.springframework.data.mapping.PropertyReferenceException: No property first found for type com.klein.springmvc1.entity.Category

这是我的自定义界面:

package com.klein.springmvc1.dao;

import com.klein.springmvc1.entity.Category;

public interface CategoryRepositoryCustom {
    Category firstCategoryByName(String catagoryName);
}

下面是这个接口的实现:

public class CategoryRepositoryCustomImpl implements CategoryRepositoryCustom {

@PersistenceContext(unitName="SpringMVC1")
EntityManager em;

private static final Logger logger = LoggerFactory.getLogger(CategoryController.class);


public Category firstCategoryByName(String catagoryName) {
    logger.debug("In my custom repo");
    Query q = em.createQuery("select category c from category where categoryName = " + catagoryName);
    @SuppressWarnings("unchecked")
    List<Category> categories = q.getResultList();

    if (categories.size() > 0) {
        return categories.get(0);
    }
    else 
        return null;
    }

}

这是我的存储库的接口定义:

    public interface CategoryRepository extends CrudRepository<Category, Long>, CategoryRepositoryCustom {

    List<Category> findByCategoryNameIgnoreCase(String catagoryName);
    @Query("From Category c where c.categoryName like ?1%")
    List<Category> findCategoryByStartsWithCategoryName(String categoryName);
    List<Category> findByParentCategoryCategoryName(String categoryName);   
}

这是我的上下文配置:

   <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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/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-3.0.xsd"
        >
    <jpa:repositories base-package ="com.klein.springmvc1.dao"/>
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!--    <beans:bean id="customCategoryRepoImpl" class="com.klein.springmvc1.dao.CustomCategoryRepoImpl">
    </beans:bean>
     -->
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven conversion-service="conversionService"  />


    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>


    <context:component-scan base-package="com.klein.springmvc1" />

    <beans:bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 
        <beans:property name="formatters"> 
        <beans:set>
            <beans:bean class="com.klein.springmvc1.MyDateAnnotationFormatterFactory"/>
            <beans:bean class="com.klein.springmvc1.DateFormatter"/> 
        </beans:set>
        </beans:property>
    </beans:bean>


    <beans:bean id="exceptionTranslator" class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />

    <beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" /> 

    <beans:bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

    <tx:annotation-driven/>

</beans:beans>

任何建议表示赞赏。

4

1 回答 1

6

默认情况下,在实现自定义时使用后缀Spring-Data-JPA查找,但由于您已命名,您将编辑您的配置为RepositoryImplRepositoryCategoryRepositoryCustomImpl

 <jpa:repositories base-package ="com.klein.springmvc1.dao" repository-impl-postfix="CustomImpl"/>

有关添加自定义行为的更多信息:http: //docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations

于 2013-09-18T17:44:45.423 回答