tuine
V2EX  ›  Java

求助:jpa 不使用原生 sql 如何实现 jsonb 字段排序?

  •  
  •   tuine · May 26, 2021 via iPhone · 2159 views
    This topic created in 1821 days ago, the information mentioned may be changed or developed.

    直接使用会报错:No property jsonbField->'field' found for type TestEntity 代码如下: Pageable pageable = PageRequest.of(1, 2, JpaSort.unsafe(Sort.Direction.ASC, "jsonbField->'field'")); Page<TestEntity> page = testRepository.findAll(pageable);

    使用 nativeQuery 结合 pageable 没有问题,现在需要复杂筛选如何使用 Criteria 查询实现?

    2 replies    2021-05-27 14:23:40 +08:00
    akiza
        1
    akiza  
       May 27, 2021
    可以试试 @Formula 注解,@Formula("JSON_EXTRACT(field, '$.filed.orderFiled')")
    tuine
        2
    tuine  
    OP
       May 27, 2021
    @akiza jsonbField 字段中的数据是可变的,所以排序的字段也不固定,如果用这个注解就需要事先定义出来
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2904 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 11:08 · PVG 19:08 · LAX 04:08 · JFK 07:08
    ♥ Do have faith in what you're doing.