无法在spring data jpa存储库上获取自定义行为

时间:2013-09-13 23:39:14

标签: spring-data-jpa

我正在尝试将自定义行为添加到基本的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:首次找不到类型为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>

任何建议表示赞赏。

1 个答案:

答案 0 :(得分:5)

默认情况下,Spring-Data-JPA在实施自定义Repository后会查找带有Impl后缀的Repository,但由于您已命名为CategoryRepositoryCustomImpl,因此您需要进行编辑您的配置为

 <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