简体   繁体   English

Jpa + Spring 引导实体过滤器

[英]Jpa + Spring Boot Entity filter

I have a JPA entity thats has a "published" fleg.我有一个 JPA 实体,它有一个“已发布”的 fleg。 For example let's describe it as TestEntity:例如,让我们将其描述为 TestEntity:

  • Long testId长 testId
  • String testDescription字符串测试描述
  • Boolean published Boolean 发布

What I want is every time I access the entity, even when it is another entity's related entity, it is accessed with publish=True.我想要的是每次访问实体时,即使它是另一个实体的相关实体,也可以使用 publish=True 访问它。 The problem is that i can not use something like @Filter(condition="published = true") because there are some cases in witch i do want to access the same entity without that filter, explicititing it in some way.问题是我不能使用@Filter(condition="published = true")类的东西,因为在某些情况下,我确实想在没有该过滤器的情况下访问同一个实体,以某种方式对其进行显式化。 Is there any simple way to do that?有什么简单的方法可以做到这一点吗?

UPDATE: I can not set it in the Repository because i need the condition to be setted also in a JOIN from another entity, that does not uses the TestEntity repository.更新:我无法在存储库中设置它,因为我还需要在来自另一个实体的 JOIN 中设置条件,该实体不使用 TestEntity 存储库。

you can declare a method in your JPARepository as follows:您可以在 JPARepository 中声明一个方法,如下所示:

 True    findByActiveTrue()  … where x.active = true
 False   findByActiveFalse() … where x.active = false

Possible use of keywords in query methods:查询方法中可能使用的关键字:

https://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/#jpa.query-methods.query-creation https://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/#jpa.query-methods.query-creation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM