postgresql - 使用 JOOQ 工具强制 PostgreSQL 类型转换
问题描述
有没有办法配置 JOOQ 工具以使用 PostgresSQL 数据库的“forcedTypes”标签将smallint转换为布尔值,而不提供 org.jooq.Converter 实现?
这是当前配置的样子:
<forcedTypes>
<forcedType>
<name>BOOLEAN</name>
<types>smallint.*</types>
</forcedType>
<forcedTypes>
正在使用 JOOQ v3.9.1。PostgreSQL v9.6.6。
不幸的是,在将信息存储到数据库中时收到了下一个异常:
Caused by: org.postgresql.util.PSQLException: ERROR: column "is_complete" is of type smallint but expression is of type boolean
还尝试使用 MySQL 数据库和类似的从 tinyint 到 Boolean 的转换工作正常,没有任何错误:
<forcedTypes>
<forcedType>
<name>BOOLEAN</name>
<types>tinyint.*</types>
</forcedType>
</forcedTypes>
解决方案
不,这不像你所期望的那样工作(也不应该)。在 jOOQ 中,如果数据库支持,则BOOLEAN
数据类型作为本地BOOLEAN
类型绑定到 JDBC,例如 PostgreSQL。
如果数据库不支持该类型(例如 MySQL/Oracle),那么 jOOQ 将绑定//0
数字值。但是您不能对原本支持类型的方言强制执行此行为。但是话又说回来,为什么不写那个转换器呢?这真的很简单。只需添加:1
NULL
BOOLEAN
<forcedTypes>
<forcedType>
<userType>java.lang.Boolean</userType>
<converter>com.example.BooleanAsSmallintConverter</converter>
<!-- A bit risky. Are all smallints really booleans in your database? -->
<types>smallint.*</types>
</forcedType>
<forcedTypes>
进而:
class BooleanAsSmallintConverter extends AbstractConverter<Short, Boolean> {
public BooleanAsSmallintConverter() {
super(Short.class, Boolean.class);
}
@Override
public Boolean from(Short t) {
return t == null ? null : t.shortValue() != (short) 0;
}
@Override
public Short to(Boolean u) {
return u == null ? null : u ? Short.valueOf((short) 1) : Short.valueOf((short) 0);
}
}
推荐阅读
- android - 无法创建布局资源文件
- reactjs - 调用 Search 函数后如何保留新组件?
- unit-testing - 如何测试回调函数?VUE/JEST
- onem2m - `child-resources` 的模式在哪里指定?
- java - 为 Minecraft Modding 设置工作场所时出错
- machine-learning - 您可以使用文本相似性搜索相关的数据库表/字段吗?
- django - Django:在下拉列表中排序项目(通过 field_order?)
- php - php如何从两个不同的关联数组中检索数据
- javascript - 在 React 中导入图像
- python - 为什么 BeautifulSoup 给我错误的文字?