【问题标题】:Spring data jpa repository property not found找不到 Spring 数据 jpa 存储库属性
【发布时间】:2015-06-19 06:41:03
【问题描述】:

我是 Spring Data JPA 的新手。我试图为存储库创建一个自定义方法,但它确实引发了异常。这是我目前的实现:

public interface EmployeeRepository extends
CrudRepository<Employee, Long>,EmployeeRepositoryCustom {

}

@Repository
public interface EmployeeRepositoryCustom {

    public void updateEmployee(String field, String value,long id);
}

public class EmployeeRepositoryImpl implements EmployeeRepositoryCustom {

    @PersistenceContext 
    private EntityManager entityManager;

    @Override
    public void updateEmployee(String field, String value, long id) {
        // Implementation goes here
    }

}

这是我启动应用程序时发生的异常(我使用的是 Spring boot)。

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property updateEmployee found for type Employee!
        at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)

【问题讨论】:

  • 来自文档:If you use namespace configuration, the repository infrastructure tries to autodetect custom implementations by scanning for classes below the package we found a repository in. 是 repo 子包中的实现吗?
  • 实现也在同一个包中
  • 这应该可以正常工作。愿意分享一个显示错误的示例项目吗?
  • 你的代码没问题。将其复制到新的 Spring Boot 项目works。您的应用程序中的其他内容正在导致问题。您是否尝试为所有存储库提供自定义实现?我在尝试为所有存储库提供自定义实现时遇到此错误。

标签: spring jpa spring-boot spring-data spring-data-jpa


【解决方案1】:

既然你已经标记了spring-boot,我假设你正在使用spring-boot。如果是这样,您可以尝试以下方法。

@Repository
public interface EmployeeRepositoryCustom {

@Modifying
@Query(value = "update Employee query", nativeQuery = true)
public void updateEmployee(String field, String value,long id);

}

您不需要实现,您可以在上面的查询中使用命名参数。 Spring-boot 将负责休息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-15
    • 2017-05-18
    • 2017-05-26
    • 2020-09-09
    • 2020-03-18
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多