自定义 JPA 存储库中的 ‘find’ 逻辑

huangapple go评论140阅读模式
英文:

Customize 'find' logic in JPA repositories

问题

我有以下问题想向社区提问:是否有办法自定义所有我的 JPA 仓库的“查找”逻辑?

例如,我希望我的自定义“查找”逻辑在每次在我的 JPA 仓库中创建包含单词“查找”的方法时自动被调用(findAll、findById、findOne 等)。

我正在尝试按照官方文档(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.customize-base-repository)进行操作,但我不确定我是否做错了什么,或者是否无法通过这种策略实现我心中所想的内容。

提前感谢您的帮助。

英文:

I have the following question for the community: is there any way to customize the 'find' logic for all my JPA repositories?

For example, I would like my custom 'find' logic to be invoked automatically everytime a method containing the word 'find' is created in my JPA repositories (findAll, findById, findOne, ..).

I'm trying to follow the official documentation (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.customize-base-repository) but I don't understand if I'm doing something wrong or if it's not possible to implement what I have in mind with this straregy.

Thank you in advance.

答案1

得分: 0

你可以使用Spring AOP来实现它(我建议你阅读文档)。当使用注解时,你的切入点表达式(在切面内部)应该如下所示:

@Pointcut("execution(*org.yourpackage.repositories.*.find*(..))")
public void customFind() {
    //...
}
英文:

You can do it with the use of Spring AOP (I suggest you read the docs). When using annotations, your pointcut expression (inside an aspect) would look like so:

@Pointcut("execution(*org.yourpackage.repositories.*.find*(..))")
public void customFind() {
    //...
}

</details>



huangapple
  • 本文由 发表于 2020年8月27日 00:08:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63601524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定