JOOQ Oracle Number精度和Java数字映射 [英] JOOQ Oracle Number precision and java number mapping

查看:286
本文介绍了JOOQ Oracle Number精度和Java数字映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我或提供oracle数字精度和Java类型之间的映射的参考,即在什么时候number(x)映射到short,int,long BigInteger等

Can anyone tell me or provide a ref to the mapping between oracle number precisions and java types ie at what point does a number(x) get mapped to a short, int, long BigInteger etc

推荐答案

Java的整数类型与Oracle的NUMBER类型不是完美的匹配.本质上,有两种方法可以在世界之间进行映射,但这两种方法都不完美:

Java's integer types are not a perfect match for Oracle's NUMBER types. Essentially, there are two ways to map between the worlds, both imperfect:

  • 现状:严格小于NUMBER(3)-> Byte.

  • The status quo: strictly less than NUMBER(3) -> Byte.

这保证了可以始终将SQL值读取为其Java类型. 某些Java值可能无法写入SQL类型.

This guarantees that a SQL value can always be read to its Java type. Some Java value might not be writable to the SQL type.

备选方案: Byte-> NUMBER(3)或更小.

这将确保始终可以将Java byte值写入数据库.不过,某些DB值可能无法读入Java类型.

This will guarantee that a Java byte value can always be written to the database. Some DB values might not be readable into the Java type, though.

jOOQ默认为第一个,原因如下:

jOOQ defaults to the first one, because of the following assumptions:

  • jOOQ通常用作数据库优先" API
  • 大多数数据是从数据库中读取的,而不是写入数据库中的

在jOOQ 3.8.4中,以下逻辑在

In jOOQ 3.8.4, the following logic is implemented in DefaultDataType.getNumericClass():

// Integers
if (scale == 0 && precision != 0) {
    if (precision < BYTE_PRECISION) {
        return Byte.class;
    }
    if (precision < SHORT_PRECISION) {
        return Short.class;
    }
    if (precision < INTEGER_PRECISION) {
        return Integer.class;
    }
    if (precision < LONG_PRECISION) {
        return Long.class;
    }

    // Default integer number
    return BigInteger.class;
}

// Non-integers
else {
    return BigDecimal.class;
}

使用:

int LONG_PRECISION    = String.valueOf(Long.MAX_VALUE).length();    // 19
int INTEGER_PRECISION = String.valueOf(Integer.MAX_VALUE).length(); // 10
int SHORT_PRECISION   = String.valueOf(Short.MAX_VALUE).length();   // 5
int BYTE_PRECISION    = String.valueOf(Byte.MAX_VALUE).length();    // 3

覆盖默认值

例如,如果在某些情况下使用NUMBER(3)来存储byte数字直到127,则可以通过在代码生成阶段指定重写数据类型来覆盖此默认值.在此处记录:

Overriding the default

If in some cases you use NUMBER(3) to store byte numbers up to 127 for instance, you can override this default by specifying data type rewriting during the code generation phase. This is documented here:

http://www.jooq.org/doc/latest/manual/code-generation/data-type-rewrites

这篇关于JOOQ Oracle Number精度和Java数字映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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