无法使Spring Data JPA存储库上的自定义行为起作用

4
我正在尝试为基本的Spring Data JPA Repository添加自定义行为,并根据此处列出的文档进行了操作: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>

任何建议都受到欢迎。
1个回答

6

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接