如何在 JDBC 模板中使用 UUID?
How do I use a UUID in a JDBC template?
我正在使用 spring 框架和 JDBC 模板,我也在使用 postgres。
我在 postgres 中有一些表使用 UUID 作为主键,并且该列的类型是 postgres 的 native UUIDs。如何将这些 UUID 存储在通过 JDBC 模板创建的准备好的语句中?
我试过将 UUID 转换为这样的字符串:
int rowsAffected = this.jdbc.update(sql, new Object[] {
baseMaterial.getId().toString().toLowerCase(),
baseMaterial.getName(),
baseMaterial.getDescription()
});
但这会导致此错误:
ERROR: column "id" is of type uuid but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
如果我像这样使用原始 UUID:
int rowsAffected = this.jdbc.update(sql, new Object[] {
baseMaterial.getId(),
baseMaterial.getName(),
baseMaterial.getDescription()
});
然后我得到了这个错误:
org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.UUID. Use setObject() with an explicit Types value to specify the type to use.
有什么想法吗?这让我发疯。
像这样在查询中使用类型:
int[] types = {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
int rowsAffected = this.jdbc.update(sql, new Object[]{
baseMaterial.getId().toString().toLowerCase(),
baseMaterial.getName(),
baseMaterial.getDescription()
}, types);//<<-----------specify the type of each attribute
您使用的 PostgreSQL 驱动程序版本已有 6 年历史,此后 changed/improved 已经有很多版本了。我建议升级到版本 42.1.4。
我扫描了 release notes,但我没有找到他们添加(或改进)对 UUID 支持的具体版本。
您可以尝试使用 Prepared Statement 并让数据库使用函数 uuid_generate_v1()
处理 uuid 创建
为了使用此功能,您首先需要通过 运行 在您的 Postgres 数据库中创建一个扩展:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
然后在您的 DAO 中您可以:
private String ADD_USER = "insert into Users(id, name, description) values (uuid_generate_v1(), ?, ?)";
jdbcTemplate.update(ADD_USER, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, name);
preparedStatement.setString(2, description);
}
});
您无需担心插入 uuid,因为数据库会使用函数 uuid_generate_v1();
为您完成此操作
我正在使用 spring 框架和 JDBC 模板,我也在使用 postgres。
我在 postgres 中有一些表使用 UUID 作为主键,并且该列的类型是 postgres 的 native UUIDs。如何将这些 UUID 存储在通过 JDBC 模板创建的准备好的语句中?
我试过将 UUID 转换为这样的字符串:
int rowsAffected = this.jdbc.update(sql, new Object[] {
baseMaterial.getId().toString().toLowerCase(),
baseMaterial.getName(),
baseMaterial.getDescription()
});
但这会导致此错误:
ERROR: column "id" is of type uuid but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
如果我像这样使用原始 UUID:
int rowsAffected = this.jdbc.update(sql, new Object[] {
baseMaterial.getId(),
baseMaterial.getName(),
baseMaterial.getDescription()
});
然后我得到了这个错误:
org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.UUID. Use setObject() with an explicit Types value to specify the type to use.
有什么想法吗?这让我发疯。
像这样在查询中使用类型:
int[] types = {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
int rowsAffected = this.jdbc.update(sql, new Object[]{
baseMaterial.getId().toString().toLowerCase(),
baseMaterial.getName(),
baseMaterial.getDescription()
}, types);//<<-----------specify the type of each attribute
您使用的 PostgreSQL 驱动程序版本已有 6 年历史,此后 changed/improved 已经有很多版本了。我建议升级到版本 42.1.4。
我扫描了 release notes,但我没有找到他们添加(或改进)对 UUID 支持的具体版本。
您可以尝试使用 Prepared Statement 并让数据库使用函数 uuid_generate_v1()
处理 uuid 创建为了使用此功能,您首先需要通过 运行 在您的 Postgres 数据库中创建一个扩展:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
然后在您的 DAO 中您可以:
private String ADD_USER = "insert into Users(id, name, description) values (uuid_generate_v1(), ?, ?)";
jdbcTemplate.update(ADD_USER, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, name);
preparedStatement.setString(2, description);
}
});
您无需担心插入 uuid,因为数据库会使用函数 uuid_generate_v1();
为您完成此操作