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

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

问题描述

我正在将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将对象保存到列,我使用休眠类型项目。
现在我想获取所有过期的 LiveToken s。 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之前注册了该函数即可。

SQL JSON functions

If you want to call a SQL function that processes the JSON column, then you just need to make sure you register the function with Hibernate prior to using it.

签出本文中提供了多个用于注册SQL函数的选项。

Check out this article for multiple options for registering a SQL function.

像PostgreSQL的JSON运算符(例如->> )一样,您唯一的选择是使用本机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.

签出本文中的示例向您展示了如何使用本地SQL查询来获取JPA实体。

Check out this article for an example showing you how to fetch JPA entities using native SQL queries.

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

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