简体   繁体   English

postgres jdbc 插入自定义类型数组

[英]postgres jdbc insert array of custom type

I want to insert array of custom type into postgres with jdbc .我想用jdbc将自定义类型的数组插入到postgres中。

my sql schema:我的 sql 架构:

CREATE TYPE element_pk_t AS (
    workspace_id   BIGINT,
    element_id     BIGINT,
    history_id     BIGINT
);

my java class:我的 java class:

public class ElementPK {
    public Long workspaceId;

    public Long elementId;

    public Long historyId;
}

How should I do this in java with jdbc ?我应该如何在javajdbc中执行此操作?

I've found tutorial regarding custom type https://docs.oracle.com/javase/tutorial/jdbc/basics/sqlcustommapping.html , but array of custom type is still unclear to me.我找到了有关自定义类型https://docs.oracle.com/javase/tutorial/jdbc/basics/sqlcustommapping.html的教程,但我仍然不清楚自定义类型的数组。

One trivial approach is using pure string style SQL statement, but I still prefer using PreparedStatement with setObject or setArray一种简单的方法是使用纯字符串样式 SQL 语句,但我仍然更喜欢将PreparedStatementsetObjectsetArray一起使用

If I understood you correctly, then, from the Java code, you need to generate query that looks, for example, like this:如果我理解正确,那么,从 Java 代码,您需要生成查询,例如,如下所示:

INSERT INTO parent_table(elements) 
VALUES (
ARRAY[
     row(1, 2, 3)::element_pk, 
     row(4, 5, 6)::element_pk, 
     row(7, 8, 9)::element_pk
]);

Assuming that you have a table like the following:假设您有一个如下表:

CREATE TABLE parent_table(
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    elements element_pk[]
)

And the Type like you described.以及您描述的类型。 If I were you, I wont implement it via an array.如果我是你,我不会通过数组来实现它。 I would have use JSONB instead - in this way you wont lose an ability to use indexing.我会改用 JSONB - 这样你就不会失去使用索引的能力。 Of course, there is java.sql.Array out there, and you can still do it via the following:当然,那里有java.sql.Array ,您仍然可以通过以下方式完成:

Array array = connection.createArrayOf("public.element_pk", new ElementPK[]{
                new ElementPK(1, 2, 3),
                new ElementPK(9, 4, 6)
        });

And then set the array, to PgPreparedStatement , but the thing is that internally PgPreparedStatement will enclose your elements within curly braces, which is ok, but each element will be represented by its toString method call result.然后将数组设置为PgPreparedStatement ,但问题是 PgPreparedStatement 内部会将您的元素括在大括号内,这没问题,但每个元素都将由其 toString 方法调用结果表示。 I mean, assume your ElementPK toString looks like this:我的意思是,假设您的 ElementPK toString 看起来像这样:

    @Override
    public String toString() {
        return "This is how it is implemented, really?";
    }

then you will get the SQL like:然后你会得到 SQL 这样的:

INSERT INTO parent_table (elements) VALUES ('{"This is how it is implemented, really?","This is how it is implemented, really?"}')

Again, it is maybe possible to adopt it, but from my perspective - at least having your logic within toString method is not that great, is it?同样,也许可以采用它,但从我的角度来看 - 至少在 toString 方法中包含你的逻辑并不是那么好,是吗? Spare yourself and do the following:放过自己并执行以下操作:

CREATE TABLE via_jsonb(
    elements JSONB
);

and then simply:然后简单地:

INSERT INTO via_jsonb VALUES(
'{
    "workspace_id" : 1,
    "element_id" : 2,
    "history_id" : 3
 }'
);

and in the Java code I would have simply create json from your object and set it as a string.在 Java 代码中,我只需从您的 object 创建 json 并将其设置为字符串。 Really, there are a lot of functions and cool features on JSONB out of the box.真的,开箱即用的 JSONB有很多功能和很酷的特性

Hope it helped, have a nice day!)希望对你有所帮助,祝你有美好的一天!)

Follow up to the last part of @misha2045 answer.跟进 @misha2045 回答的最后一部分。

If you still refuese to use JSONB for whatever reason you need to add parenthesis at the end and start of your toString method.如果出于任何原因您仍然拒绝使用 JSONB,则需要在 toString 方法的末尾和开头添加括号。

@Override
public String toString() {
    return "("+this.field1+ ", " + this.field2+")";
}

Then set the array in the prepared statement然后在准备好的语句中设置数组

ps.setArray(1, con.createArrayOf("yourType", yourClassArray));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM