java - 无法使用自定义生成器策略为 getter 名称创建记录
问题描述
我在 jOOQ 3.13.1,dropwizard 2.0.7。为了一起制作 jOOQ 和 dropwizard,我正在使用(https://droptools.bendb.com/jooq/)。我正在使用自定义生成策略来为我的 setter 和 getter 维护驼峰式案例。名字按预期出现。
记录对象具有各自列的数据。但是,我不断从我的数据库中收到错误,我试图在非空列上设置“空”。
我仅在尝试创建新记录时才看到此问题。更新记录工作得很好。
ERROR [2021-03-18 14:58:05,363] com.bendb.dropwizard.jooq.jersey.LoggingDataAccessExceptionMapper: Error handling a request: 9f65c0316d996ebb
! org.postgresql.util.PSQLException: ERROR: null value in column "createdAt" of relation "failures" violates not-null constraint
! Detail: Failing row contains (265, null, Some callback, User account not found, null, null, null).
如果我打印记录,它看起来像这样:
+------+------+--------------+--------------------------------------------------+------------------------------+------------------------------+------+
| id|userId|action |error |createdAt |updatedAt |status|
+------+------+--------------+--------------------------------------------------+------------------------------+------------------------------+------+
|{null}|{null}|*Some callback|*User account not found|*2021-03-18 14:58:05,363|*2021-03-18 14:58:05,363|{null}|
+------+------+--------------+--------------------------------------------------+------------------------------+------------------------------+------+
我的吸气剂名称是:“getId”、“getUserId”、“getAction”、“getError”、“getCreatedAt”、“getUpdatedAt”、“getStatus”。
对于小写的列,我看不到任何问题。如果对于列名在 CamelCase 中的地方,问题。
该类看起来像:
public class FailureDao {
private final DSLContext database;
public FailureDao(DSLContext database) {
this.database = database;
}
public void storeFailure(FailureRecord failure) {
database.newRecord(FAILURES, failure).store();
}
}
对于代码生成,我正在关注这里的文档https://www.jooq.org/doc/3.13/manual/code-generation/codegen-generatorstrategy/
我的生成器类看起来像:
public class AsInDatabaseStrategy extends DefaultGeneratorStrategy {
@Override
public String getJavaIdentifier(Definition definition) {
return definition.getOutputName().toUpperCase();
}
@Override
public String getJavaSetterName(Definition definition, Mode mode) {
return "set" + StringUtils.toUC(definition.getOutputName());
}
@Override
public String getJavaGetterName(Definition definition, Mode mode) {
return "get" + StringUtils.toUC(definition.getOutputName());
}
}
解决方案
我发现了这个问题。事实证明,这是在https://groups.google.com/g/jooq-user/c/1iy0EdWe_T8/m/YN9PEsIF4lcJ上解释的。我的解决方法是使用 jOOQ 生成的 POJO。为了创建新记录,我现在传递的是 POJO 类的对象,而不是传递 Record 类的对象。
推荐阅读
- javascript - React:刷新时如何持久化数据?渲染时从 API 检索数据
- mysql - 如何在 mysql 查询中返回 0 而不是 null?
- javascript - 在 React 应用程序中导入 mongoose npm 模块时出现 TypeError
- dependency-injection - ASP.NET Core 3.0 - 无法访问已处置的对象。对象名称:'IServiceProvider'
- python - PyQt QGridLayout() 不当行为
- jquery - 为什么我的按钮在添加类后不会改变状态?
- javascript - “pick”函数的 TypeScript 泛型类型(结果对象值类型)
- javascript - 关于javascript中同步和异步回调的说明
- apache-spark - 通过安装在 Anaconda 中的 Pyspark 从 GCS 存储桶中读取 csv 文件
- c# - 像 SQL Server EncryptByPassPhrase 一样从 C# 加密?