Spring Data JPA存储库属性未找到。

4

我刚接触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)

从文档中得知:如果您使用命名空间配置,则存储库基础结构会尝试通过扫描在我们发现存储库的包下面的类来自动检测自定义实现。 实现是否在存储库的子包中? - Evgeni Dimitrov
同一包中也要实现。 - Shri
那应该可以直接运行。能否分享一个示例项目,展示出现的错误? - Oliver Drotbohm
你的代码没问题。将其复制到一个新的Spring Boot项目中是可行的。你的应用程序中有其他问题导致错误。您是否尝试为所有存储库提供自定义实现?我在尝试为所有存储库提供自定义实现时遇到了这个错误。 - manish
1个回答

0

由于您标记了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 将处理其余部分。


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