【问题标题】:Convert Hibernate @Formula (case ) to JOOQ field将 Hibernate @Formula (case) 转换为 JOOQ 字段
【发布时间】:2016-05-15 03:07:14
【问题描述】:

我正在将整个数据库访问层从 Hibernate 重写为 JOOQ,我面临以下问题。

其中一个 JPA 模型使用@Formula 注解进行如下注解:

@Formula("(case"
    + "  when field1 >= 0.5 then 2"
    + "  when field1 >= 0.2 then 1"
    + "  else 0 end)")
private int field2;

我看到了以下问题: Convert Hibernate @Formula to JOOQ field 但它并没有真正帮助

如何将上述查询转换为 JOOQ DSL?

【问题讨论】:

    标签: sql hibernate dsl jooq


    【解决方案1】:

    jOOQ 手册中关于CASE 表达式的部分将对您有所帮助:

    http://www.jooq.org/doc/latest/manual/sql-building/column-expressions/case-expressions

    基本上,您应该构建的表达式如下所示:

    Field<Integer> field2 = 
    DSL.when(TABLE.FIELD1.ge(0.5), 2)
       .when(TABLE.FIELD1.ge(0.2), 1)
       .otherwise(0);
    

    当然,jOOQ 不是基于注解的 API,因此您不能使用注解在目标实体上自动生成该值。就像在 SQL 中一样,您将在所有相关查询中使用上述 field2 表达式,例如

    DSL.using(configuration)
       .select(TABLE.ID, TABLE.FIELD1, field2)
       .from(TABLE)
       .fetch();
    

    【讨论】:

    • 工作正常,但 DSL 和 DSLcontext 有什么区别?
    • DSL 包含静态 DSL 方法,而 DSLContext 包含 Configuration 上下文中的 DSL 方法。 More info here
    猜你喜欢
    • 2015-03-25
    • 2016-01-26
    • 2020-06-08
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 2013-08-26
    • 2016-05-15
    • 2012-12-16
    相关资源
    最近更新 更多