【问题标题】:Spring Data JPA Custom RepositorySpring Data JPA 自定义存储库
【发布时间】:2018-10-06 12:15:23
【问题描述】:

我在尝试使用 Spring Data JPA 实现自定义存储库时遇到了一些问题。

我尝试遵循一些类似这样的参考指南,但我找不到问题: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations

我不知道为什么 Spring 会尝试将自定义存储库方法查找为我的实体属性。

spring-boot-starter-data-jpa => 1.5.11.RELEASE

实体:

public class MyEntity {

    private Integer id
    private String name;

    // Getter/Setters...
}

服务:

@Service
public class MyServiceImpl implements MyService {


    @Autowired
    private MyRepository repository;

    //...

    public MyDTO customFind(Long id){


        return repository.customFind(id);
    }
}

存储库:

@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long>, QueryDslPredicateExecutor<MyEntity>, MyCustomRepository {

    //no-op
}

自定义存储库:

public interface MyCustomRepository {

    List<MyDTO> customFind(Long id);

}

自定义存储库实现:

public class MyCustomRepositoryImpl implements MyCustomRepository {


    @Autowired
    private EntityManager entityManager;


    public List<MyDTO> customFind(Long id){


        JPAQuery<EmpregadoEntity> query = new JPAQuery<>(entityManager);
        MyDTO myDTO = query...  //... JPA query return MyDTO

        return myDTO;

    }
}

当我运行应用程序时,我得到了 PropertyReferenceException:

例外:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property customFind found for type MyEntity!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:79)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:335)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:311)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:274)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(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.<init>(PartTree.java:378)
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:86)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:70)

【问题讨论】:

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


    【解决方案1】:

    我只是将类“MyCustomRepository”重命名为“MyRepositoryCustom”来解决问题。

    现在可以正常工作了! =)

    【讨论】:

      【解决方案2】:

      它使用的实体类型和 ID,MyEntity 和 Long,在 JpaRepository 的通用参数中指定。这就是您的自定义存储库应该使用的。所以你的MyCustomRepositoryImpl 应该使用 MyEntity 而不是 MyDTO

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-02
        • 2021-04-01
        • 1970-01-01
        • 2017-09-19
        • 2014-02-02
        • 1970-01-01
        • 1970-01-01
        • 2015-09-28
        相关资源
        最近更新 更多