V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
tuine
V2EX  ›  Java

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

  •  
  •   tuine · May 26, 2021 via iPhone · 2111 views
    This topic created in 1807 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   ·   2931 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 15:27 · PVG 23:27 · LAX 08:27 · JFK 11:27
    ♥ Do have faith in what you're doing.