代码之家  ›  专栏  ›  技术社区  ›  javahippie

JPA-在where子句中使用“复合值”查询

  •  0
  • javahippie  · 技术社区  · 9 年前

    我想在JPA 2.1中实现以下(例如:MySQL)语句,并使用Eclipselink作为实现:

    select *
    from account
    where (mailing_zip_code, id) > (56237, 275)
    order by mailing_zip_code asc, id asc 
    limit 10;
    

    我想实现的是一个seek方法的实现,用于无休止的滚动,而不在查询中使用偏移量。重点放在where子句上,我无法找到此构造的正确名称。在某些地方,它被称为“复合值”,但我无法用这个名称找到结果,但它似乎符合SQL-92标准。

    此语句将通过过滤器参数进行扩展,因此我自然希望使用JPA标准API构建它。

    我搜索API已经有一段时间了,现在有人知道这是可能的吗?

    1 回复  |  直到 9 年前
        1
  •  0
  •   javahippie    9 年前

    在意识到这一点之后

    select *
    from account
    where (mailing_zip_code, id) > (56237, 275)
    order by mailing_zip_code asc, id asc 
    limit 10;
    

    只是一种更短的写作方式

    select *
    from account
    where mailing_zip_code > '56237'
    or (mailing_zip_code = '56237' AND id > 276)
    order by mailing_zip_code asc, id asc 
    limit 10;
    

    条件查询毕竟没有那么难(只附加了谓词):

    if (null != startId) {
         Predicate dateGreater = cb.greaterThan(entity.get(sortBy), startValue);
    
         Predicate dateEqual = cb.equal(entity.get(sortBy), startValue);
         Predicate idGreater = cb.greaterThan(entity.get("id"), startId);
         Predicate dateEqualAndIdGreater = cb.and(dateEqual, idGreater);
    
         cb.or(dateGreater, dateEqualAndIdGreater);
    }
    

    如果有更好的方法,我会很高兴了解它。