java - 无法在 Spring Data Repository 中创建自定义查询方法

标签 java hibernate spring-boot spring-data-jpa jhipster

<分区>

我想创建自定义存储库:

public interface FriendRepositoryCustom {

    Page<Friend> findFriends(FriendCriteria friendCriteria, Pageable pageable);
}

及其实现:

@Repository
@Transactional(readOnly = true)
public class FriendRepositoryCustomImpl implements FriendRepositoryCustom {

    @PersistenceContext
    EntityManager entityManager;

    @Override
    public Page<Friend> findFriends(FriendCriteria friendCriteria, Pageable pageable) {
    ...
    }

并将其添加到主存储库:

@Repository
public interface FriendRepository extends JpaRepository<Friend, Long>, JpaSpecificationExecutor<Friend>, FriendRepositoryCustom {

}

当我启动应用程序时出现此错误:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findFriends found for type Friend! at org.springframework.data.mapping.PropertyPath.(PropertyPath.java:77) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) at org.springframework.data.repository.query.parser.Part.(Part.java:76) at org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:247) at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) at org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:378) at org.springframework.data.repository.query.parser.PartTree.(PartTree.java:86) at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:70) ... 43 common frames omitted

最佳答案

您可能错误地命名了您的实现类。

请注意,命名预期随 Spring Data 2.0 发生了变化。

对于 < 2.0,必须将实现命名为带有附加 Impl 后缀的最终存储库接口(interface)。 See the matching reference documentation for an example .

对于 >= 2.0,实现必须命名为带有附加 Impl 后缀的自定义接口(interface)。 See the current reference documentation for an example .

注意:您不需要任何 @Repository 注释。

关于java - 无法在 Spring Data Repository 中创建自定义查询方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52624486/

相关文章:

java - 工作服徽章显示为 'unknown'

spring - dockerized 环境中的 Keycloak 和 Spring Boot Web 应用程序

java - 在库中使用 realm.io 时出现 NoSuchMethodError

java - 每个子类都有表的 Hibernate 鉴别器列

java - 如何在 Hibernate 中处理命名 native 查询中的额外字段

java - Hibernate - 与列名的一对一映射

java - Spring Security 在 Spring Boot 中检索用户名时提供 Null 身份验证

java - 我可以在处理程序/可运行对象中执行网络操作(UI 阻塞)吗?

java - hibernate :刷新、驱逐、复制和刷新

java - 即使两个字符串 s1 和 s3 具有相同的哈希码,== 如何返回 false?