Hibernate @formula is not supportinng Cast() as int for teradata database

后端 未结 2 1232
天涯浪人
天涯浪人 2020-12-07 04:14

I am using

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

to cast a String column to int

but hiberna

相关标签:
2条回答
  • 2020-12-07 04:18

    Try:

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

    The problem is that the dialect you are using does not recognize int as a Type (see org.hibernate.dialect.isTypeNameRegistered()).

    You can either use the exact type based on your dialect (e.g. PostgreSQL uses int4 but DB2, Ingres and H2 uses integer ...) or you can use NUMERIC(1,0) because everything what is followed by parentheses is considered to by a Function or a Keyword (see org.hibernate.sql.Template.isFunctionOrKeyword()).

    0 讨论(0)
  • 2020-12-07 04:22

    I have resolved an issue similar to yours. I extended the hibernate Dialect class for my database.

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

    Set 'hibernate.dialect' property with this new class. Now your @Formula will work.

    0 讨论(0)
提交回复
热议问题