简体   繁体   English

对象序列化期间的DbUtils类型转换问题

[英]DbUtils type conversion issue during object serialization

I am using DbUtils to handle interchangeability between PostgreSQL and MySQL. 我正在使用DbUtils来处理PostgreSQL和MySQL之间的可互换性。 I kind of expected this to be an issue but was hoping there would be a clean way to handle type conversion. 我有点期待这是一个问题,但希望有一个干净的方式来处理类型转换。 I am using Flyway to do my migrations, so I have to write my schema as standard as possible to support all the different types of relational databases it supports, with PostgreSQL and MySQL being a primary focus (for now). 我正在使用Flyway进行迁移,因此我必须尽可能地编写我的模式以支持它支持的所有不同类型的关系数据库,PostgreSQL和MySQL是主要关注点(目前)。

Here is a basic schema that I am working with: 这是我正在使用的基本架构:

CREATE TABLE access (
  id SERIAL PRIMARY KEY,
  apikey varchar(36) NOT NULL,
  apikey_type int DEFAULT '0',
  apikey_enabled smallint DEFAULT '1',
  topicid int DEFAULT NULL,
  collection varchar(60) DEFAULT NULL,
  lastseen timestamp NULL DEFAULT NULL,
);

Here is what the POJO class looks like: 这是POJO类的样子:

public class ClientAccessRecord {

    private BigInteger id;

    private Integer apiKeyType;

    private boolean enabled;

    private String topic;

    private int topicId;

    private String lastRecordTime;

    private int lastRecordId;

    private String collection;
}

I am struggling with two fields: id and apikey_enabled . 我正在努力争取两个领域: idapikey_enabled I have specified id to be SERIAL because PostgreSQL does not support AUTO_INCREMENT directive. 我已将id指定为SERIAL因为PostgreSQL不支持AUTO_INCREMENT指令。 However, SERIAL means UNSIGNED INT in PostgreSQL and UNSIGNED BIGINT in MySQL, resulting in a conversion failure in DbUtils BeanHandler . 但是, SERIAL表示PostgreSQL中的UNSIGNED INT和MySQL中的UNSIGNED BIGINT ,导致DbUtils BeanHandler中的转换失败。 Furthermore, the apikey_enabled fails in PostgreSQL but not in MySQL. 此外, apikey_enabled在PostgreSQL中失败但在MySQL中失败。 It treats it as a boolean in MySQL while PostgreSQL is trying to convert into an int. 它将它视为MySQL中的布尔值,而PostgreSQL试图转换为int。

I am at a loss here. 我在这里不知所措。 What are the best practices when trying to standardize schema's? 尝试标准化架构时的最佳做法是什么? Should I avoid DbUtils object mapping and stick to the more tedious, albeit more control, approach of manually setting these values? 我是否应该避免使用DbUtils对象映射并坚持使用手动设置这些值的更繁琐(虽然更多控制)的方法?

You don't have to use SERIAL in PostgreSQL as per: 您不必在PostgreSQL中使用SERIAL

https://stackoverflow.com/a/6632280 https://stackoverflow.com/a/6632280

Which means you can use any data type... 这意味着您可以使用任何数据类型......

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

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