Hibernate @formula不支持将Cast()转换为Teradata数据库的int类型

6

我正在使用

@Formula("cast(item_code as \"int\")")

将一个 String 列转换为 int

但是 hibernate 生成的查询语句为

and cast(this_.item_code as this_."int")=? 

我该如何去掉 int 前面的 alias ?

我尝试过:

@Formula("cast(item_code as "int")")@Formula("cast(item_code as int)")

但仍然有相同的错误。我该如何将 String 转换为 int

先行感谢。

2个回答

9

我曾解决过类似于你的问题。我扩展了我的数据库的Hibernate方言类。

public class Oracle10gDialectExtended extends Oracle10gDialect {

    public Oracle10gDialectExtended() {
        super();
        /* types for cast: */
        registerKeyword("int");
        // add more reserved words as you need
    }
}

使用这个新类设置'hibernate.dialect'属性。现在你的@Formula将会起作用。


我有一个包含子查询的 @Formula,其中使用了 listagg(...) within group ...。我曾经遇到过类似于 within 子句的问题,但是使用 registerKeyword("within") 并没有起到帮助作用。最终,我使用自己的字符串聚合函数来解决了这个问题,没有深入挖掘这种 Hibernate 行为的原因。 - Tomáš Záluský
并且注册时全部使用小写字母。浪费了两个小时来调试这个问题。 - leoismyname
对应的Spring Boot属性:spring.jpa.database-platform - undefined

0

尝试:

@Formula("cast(item_code as NUMERIC(1,0))")

问题在于你使用的方言不认识int作为一种类型(参见org.hibernate.dialect.isTypeNameRegistered())。
你可以使用基于你的方言的确切类型(例如 PostgreSQL 使用 int4,但 DB2、Ingres 和 H2 使用 integer...),或者你可以使用NUMERIC(1,0),因为括号后面的所有内容都被视为函数或关键字(参见org.hibernate.sql.Template.isFunctionOrKeyword())。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接