java - JDBCIO 调用 Postgres 例程(存储过程),它将自定义对象类型作为参数

标签 java postgresql jdbc apache-beam jdbc-postgres

我正在尝试调用一个将自定义对象类型作为参数的 Postgres 例程。

create type person_type as
(
  first        varchar,
  second varchar,
  is_real     boolean
);

我的例程(存储过程):

create function person_routine(person person_type)
returns void
language plpgsql
as $$

BEGIN
  INSERT INTO person(first, second, is_real) VALUES 
(person.first,person.second,person.is_real);

END;
$$;

然后我尝试创建一个 Java 类来表示自定义类型:

import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;

public class PersonType implements SQLData {

public String first;
public String second;
public boolean is_real;
private String sql_type;

@Override
public String getSQLTypeName() throws SQLException {
    return sql_type;
}

@Override
public void readSQL(SQLInput stream, String typeName) throws SQLException {
    sql_type = typeName;
    second = stream.readString();
    first = stream.readString();
    is_real = stream.readBoolean();
}

@Override
public void writeSQL(SQLOutput stream) throws SQLException {
    stream.writeString(first);
    stream.writeBoolean(is_real);
    stream.writeString(second);
}
}

然后我尝试执行这样的代码:

.apply(JdbcIO.<Person>write()
                            .withDataSourceConfiguration(JdbcIO.DataSourceConfiguration.create(
                                    "org.postgresql.Driver", configuration.GetValue("postgres_host"))
                                    .withUsername(configuration.GetValue("postgres_username"))
                                    .withPassword(configuration.GetValue("postgres_password")))
                            .withStatement("SELECT person_routine(?)")
                            .withPreparedStatementSetter(new JdbcIO.PreparedStatementSetter<Person>() {
                                public void setParameters(Person element, PreparedStatement query)
                                        throws SQLException {
                                    PersonType dto = new PersonType();
                                    dto.first = element.first;
                                    dto.second = element.second;
                                    dto.is_real = element.is_real;
                                    query.setObject(1, dto);
                                }
                            })
                    );

不幸的是,这给了我一个异常(exception):

java.lang.RuntimeException: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of dto.PersonType. Use setObject() with an explicit Types value to specify the type to use.

任何帮助都会很棒。

最佳答案

因此,它是关于使用 PGobject() 的。这就是我实现它的方式,它似乎工作得很好。

 PGobject person = new PGobject();
 person.setType("person_type");
 person.setValue(String.format("(%s,%s,%s)","something","something","FALSE"));
 query.setObject(1, person);

关于java - JDBCIO 调用 Postgres 例程(存储过程),它将自定义对象类型作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55366808/

相关文章:

Java DB 客户端-服务器技术 - 集中式 DB - 如何?

java - 每次启动应用程序时,Spring data.sql 文件都会添加一条新记录。

postgresql - 无法单独获取所有列值,而是获取一列中的所有值 postgresql --9.5

java - 如何在 Linux 上使用用户名和密码连接到我的 mysql 数据库

java - 如何解决这个 java.sql.SQLException : Statement parameter <number> not set

java - 有没有办法使用 Apache POI 为 docx 文件设置固定元数据?

java - Android 上设置参数后启动相机失败

eclipse - java.lang.IllegalStateException : No supported DataSource type found 错误

java - IBM Notes Java 脚本库或包

postgresql - 使用 UNION ALL 从多个表中获取行或在生产中使用一个表?