带有QueryDSL的Postgresql数组函数 [英] Postgresql Array Functions with QueryDSL

查看:111
本文介绍了带有QueryDSL的Postgresql数组函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Vlad Mihalcea的库将SQL数组(以我的情况为Postgresql)映射到JPA。然后,让我们想象我有一个实体,例如。

I use the Vlad Mihalcea's library in order to map SQL arrays (Postgresql in my case) to JPA. Then let's imagine I have an Entity, ex.

@TypeDefs(
{@TypeDef(name = "string-array", typeClass = 
StringArrayType.class)}
)
@Entity
public class Entity {
    @Type(type = "string-array")
    @Column(columnDefinition = "text[]")
    private String[] tags;
}

适当的SQL是:

CREATE TABLE entity (
    tags text[]
);

使用QueryDSL我想获取包含所有给定标签的行。原始SQL可能是:

Using QueryDSL I'd like to fetch rows which tags contains all the given ones. The raw SQL could be:

SELECT * FROM entity WHERE tags @> '{"someTag","anotherTag"}'::text[];

(摘自: https://www.postgresql.org/docs/9.1/static/functions-array.html

是否可以使用QueryDSL?像下面这样的代码?

Is it possible to do it with QueryDSL? Something like the code bellow ?

predicate.and(entity.tags.eqAll(<whatever>));


推荐答案


  1. 第一步是生成适当的sql: WHERE标签@> '{ someTag, anotherTag}':: text [];

  2. 第二步由coladict描述(非常感谢!):弄清楚被调用的函数:@>是arraycontains,而:: text []是string_to_array

  3. 第三步是正确调用它们。经过数小时的调试,我发现HQL不会将函数视为函数,除非我添加了表达式符号(在我的情况下:... = true),所以最终的解决方案如下所示: predicate。和(Expressions.booleanTemplate( arraycontains({0},string_to_array({1},','))= true,entity.tags,tagsStr));
    其中 tagsStr -是 String ,其值用
  1. 1st step is to generate proper sql: WHERE tags @> '{"someTag","anotherTag"}'::text[];
  2. 2nd step is described by coladict (thanks a lot!): figure out the functions which are called: @> is arraycontains and ::text[] is string_to_array
  3. 3rd step is to call them properly. After hours of debug I figured out that HQL doesn't treat functions as functions unless I added an expression sign (in my case: ...=true), so the final solution looks like this: predicate.and(Expressions.booleanTemplate("arraycontains({0}, string_to_array({1}, ','))=true", entity.tags, tagsStr)); where tagsStr - is a String with values separated by ,

这篇关于带有QueryDSL的Postgresql数组函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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