当为Table Entity中的所有find方法设置了全局WHERE子句时,使用JPA语句来查询数据

1sbrub3j  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(77)

我在我的表实体中使用以下代码来过滤掉所有is_pooled = true的记录。(@Where(clause = "is_pooled = false")

@Entity
@Table(name = "bbser_inventory")
@Getter
@Setter
@NoArgsConstructor
@Where(clause = "is_pooled = false")
public class InventoryEntity extends BaseEntity {

字符串
但是有一个例外情况,我需要在数据库中检索is_pooled值设置为true的数据。我如何实现这一点?
我已经试过写以下解决方案,但不检索数据

方法一

List<InventoryEntity> findAllByInventoryIdInAndIsPooledTrue(List<Long> inventoryIdList);

方法二

@Query("SELECT i FROM InventoryEntity i WHERE i.inventoryId IN (?1) AND i.isPooled = true")
List<InventoryEntity> findInventoryByIdAndIsPooled(Collection<Long> inventoryIds, Boolean ispooled);


下面是我的serviceImpl方法,我试图检索此数据

@Override
    public ResponseEntity<?> getPooledParentInfo(Long inventoryId) {
        PooledBloodInventoryEntity pooledBloodInventoryEntity = pooledInventoryRepository.findDistinctFirstByInventoryId(inventoryId);
        List<Long> pooledInventoryIdList = pooledBloodInventoryEntity.getParentInventoryIds();
        List<InventoryEntity> inventoryEntityList = inventoryRepository.findInventoryByIdAndIsPooled(pooledInventoryIdList, true);

        return new ResponseEntity<>(inventoryEntityList, new HttpHeaders(), HttpStatus.OK);
    }

col17t5w

col17t5w1#

下面的例子可能会很有用。@在原生查询中注解不起作用的地方

@Query(value = "SELECT i.* FROM bbser_inventory i WHERE i.inventory_id IN (:inventoryIds) and i.is_pooled = true", nativeQuery = true)
List<InventoryEntity> findInventoryByIdAndIsPooled(Collection<Long> inventoryIds);

字符串

相关问题