如何在 jsonb 列上使用 Spring JPA 进行查询? [英] How to query with Spring JPA on jsonb columns?

查看:86
本文介绍了如何在 jsonb 列上使用 Spring JPA 进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 spring JPA 与 PostgreSQL 数据库一起使用.我有一个实体如下:

I'm using spring JPA with PostgreSQL database. I have an Entity as follow:

@Entity
@TypeDef(name="json_binary", typeClass = com.vladmihalcea.hibernate.type.json.JsonBinaryType.class)
public class LiveTokens {

   @Id
   @GeneratedValue()
   private Long id;

   private String username;

   @Type(type="json_binary")
   @Column(columnDefinition = "jsonb")
   private Token token
}

令牌:

public class Token {
   private Long expireTime;
   private String accessToken;
}

为了使用 Hibernate 将对象保存到列,我使用了 Hibernate Types 项目.现在我想获取所有过期的 LiveToken .我不能用 Spring JPA 做到这一点.如何使用 Spring 数据 JPA 查询 posgresql jsonb 列?

For saving object to column with Hibernate, I use Hibernate Types project. Now I want to get All LiveTokens that expired. I can't do it with Spring JPA. How do I query to posgresql jsonb column with Spring data JPA?

推荐答案

SQL JSON 函数

如果你想调用一个处理JSON列的SQL函数,那么你只需要确保你在使用之前向 Hibernate 注册该函数.

对于 JSON 运算符,例如 PostgreSQL 运算符(例如,->>),您唯一的选择是使用本机 SQL 查询.JPQL 或 HQL 不支持特定于数据库的 JSON 运算符.

For JSON operators, like the PostgreSQL ones (e.g., ->>), the only option you have is to use a native SQL query. JPQL or HQL don't support database-specific JSON operators.

这篇关于如何在 jsonb 列上使用 Spring JPA 进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆