gpt4 book ai didi

java - JPA 空参数

转载 作者:行者123 更新时间:2023-12-01 17:47:42 25 4
gpt4 key购买 nike

我需要获取与参数匹配的所有行,或者如果参数为空则获取所有行

我(没有)找到读取 Spring Data 的解决方案 documentation - 示例 21. 使用不同的可空性约束

尝试使用注释@Nullable的示例,但仍然不起作用,代码如下

// just a part of entity LotMaster 
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "lot_master_gen")
@SequenceGenerator(name = "sq_lot_master_gen", sequenceName = "sq_lot_master", allocationSize = 1)
@Column(name = "lot_id")
private Long id;

//repository extends JpaRepository<LotMaster, Long>
@Nullable
Page<LotMaster> findAllById(@Nullable Long id, Pageable p);

查询:

SELECT
*
FROM
(
SELECT
lot0_.lot_id AS lot1_2_,
lot0_.qty AS qty_2_,
lot0_.version AS version8_2_,
lot0_.username AS username15_2_
FROM
lot_master lot0_
WHERE
lot0_.lot_id IS NULL
)
WHERE
ROWNUM <= ?

如您所见,它使用 null 值作为条件而不是忽略它

Pic Documentation

怎么可能呢?没有 CriteriaApi 或使用 @Query 的任何想法我的想法是添加更多参数并使用派生查询

可能相关DATAJPA-209

I realize that using id as example it confuses, sorry for that. However If I use another field like qty I should have the possibility to search even with null. In a form multiple fields that can be left empy. Would be nice an implementation like @RequestParam with required or defaultValue parameters or simply @Nullable working

最佳答案

您必须findAllByIdOrId(@Nullable Long id, Long id2)并将null作为参数之一传递。使用 @Query 有点沉闷但更容易完成。

另一种方法是执行 findAllByIdOrIdIsNull(Long id) 但这会产生

关于java - JPA 空参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60836818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com