hibernate - 如何为 Spring Data JPA 中的所有查询创建动态 WHERE 子句?

标签 hibernate spring-data-jpa spring-data

我有一个要求,我需要将全局 WHERE 条件动态传递给所有查询?

我们在表中有多个组织的数据。因此,我需要在检索数据时传递 ORG_ID = ?

我不想编写像 findBy..AndOrgId(... Integer orgId) 这样的查找器方法。有一个更好的方法吗?要应用全局 WHERE 子句还是谓词?

我在 RestController 中收到此 orgId 作为请求属性的一部分。因为它是动态的,所以我不能在实体类上使用 hibernate 注释 @Where

最佳答案

您可以尝试使用hibernate @Filter 注解。正如 documentation 中所述:

The @Filter annotation is another way to filter out entities or collections using custom SQL criteria. Unlike the @Where annotation, @Filter allows you to parameterize the filter clause at runtime.

示例 1:

import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;

@Entity
@Table(name = "MY_ORDER")
@FilterDef(
   name="byId",
   parameters = @ParamDef(
      name="ordId",
      type="long"
   )
)
@Filter(
   name="byId",
   condition="ord_id = :ordId"
)
public class MyOrder
{
   @Id
   @Column(name = "ord_id")
   private Long id;

   // ...
}

和用法:

EntityManager em = emFactory.createEntityManager();
// ...
em.unwrap(Session.class).enableFilter("byId").setParameter("ordId", 2L);

List<MyOrder> orders = em.createQuery("select o from MyOrder o", MyOrder.class)
   .getResultList();

将生成如下sql:

/* select o from MyOrder o */
select
  myorder0_.ord_id as ord_id1_3_,
  myorder0_.ord_time as ord_time2_3_ 
from TEST_SCHEMA.MY_ORDER myorder0_ 
where myorder0_.ord_id = ?

编辑

上述方法也适用于 spring data jpa @Query 和从方法名称派生的查询。

示例 2:

@Entity
@Table(name = "post")
@FilterDef(
   name="postById",
      parameters = @ParamDef(
         name="postId",
         type="long"
      )
   )
@Filter(
   name="postById",
   condition="id = :postId"
)
public class Post
{
   @Id
   private Long id;

   @Enumerated(EnumType.ORDINAL)
   private PostStatus status;

   // getters, setters
}

public enum PostStatus { OPEN, CLOSE }

public interface PostRepository extends JpaRepository<Post, Long>
{
   @Query(value = "select p from Post p where p.status = :sts")
   List<Post> test(@Param("sts") PostStatus status);

   List<Post> findByStatus(PostStatus status);
}

@RestController
public class TestRestController
{
   @Autowired
   EntityManager entityManager;

   @Autowired
   private PostRepository postRepository;

   @RequestMapping("/")
   @Transactional
   public String home()
   {
      entityManager
          .unwrap(Session.class)
          .enableFilter("postById")
          .setParameter("postId", 10L);
       // ....
       List<Post> posts1 = postRepository.test(PostStatus.OPEN);
       List<Post> posts2 = postRepository.findByStatus(PostStatus.OPEN);
   }      
}

关于hibernate - 如何为 Spring Data JPA 中的所有查询创建动态 WHERE 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63742657/

相关文章:

hibernate - 在同一个查询中混合使用 HQL 和 SQL

java - 如何在 Grails 或 Hibernate 中缩短它

java - JPA/Hibernate 无法创建名为 Order 的实体

spring - 无法读取 HTTP 消息 : org. s.HttReadableException : Could not read document: Unrecognized token 'PUT' : was expecting ('true' , 'false' 或 'null')

mysql - spring batch - 如何在应用程序运行时传递动态列表到 ListItemReader 将其存储到数据库中

java - 如果实体不存在,如何告诉 Spring Data-repository delete 方法不抛出异常?

java - Hibernate 7.0.2 : javax. 验证.NoProviderFoundException

java - 获取 InvalidDataAccessApiUsageException : Path '%s' from root %s must not span a cyclic property reference when using ExampleMatcher

spring - 系统属性移至 application.properties 后停止生效

java - 由 : org. hibernate.hql.internal.ast.QuerySyntaxException : unexpected token: order near line 1, 第 17 列引起,原因绝对不清楚