【问题标题】:Can't create custom query method in Spring Data Repository [duplicate]无法在 Spring Data Repository 中创建自定义查询方法 [重复]
【发布时间】:2019-03-08 12:48:06
【问题描述】:

我想创建自定义存储库:

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 {

}

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

原因: org.springframework.data.mapping.PropertyReferenceException:否 为类型 Friend 找到属性 findFriends!在 org.springframework.data.mapping.PropertyPath.(PropertyPath.java:77) 在 org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) 在 org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) 在 org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) 在 org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) 在 org.springframework.data.repository.query.parser.Part.(Part.java:76) 在 org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:247) 在 org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) 在 org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:378) 在 org.springframework.data.repository.query.parser.PartTree.(PartTree.java:86) 在 org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:70) ...省略了43个常用框架

【问题讨论】:

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


    【解决方案1】:

    您可能将实现类命名错误。

    请注意,命名期望随着 Spring Data 2.0 的变化而改变。

    对于 Impl 后缀。 See the matching reference documentation for an example.

    对于 >= 2.0,必须将实现命名为自定义接口,并附加一个 Impl 后缀。 See the current reference documentation for an example.

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

    【讨论】:

      猜你喜欢
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 2019-01-16
      • 1970-01-01
      • 2019-03-08
      • 2016-05-07
      • 2017-08-15
      相关资源
      最近更新 更多