Grim
Grim

Reputation: 2030

HQL generate wrong cast

Having a cast like this:

SELECT
    CAST(a * 100000000 as big_integer)
FROM
    Amount
WHERE
    id = :id

Generate this SQL:

prepareStatement(
   "select cast(amount0_.\"a\"*100000000 as numeric(19, 2)) from \"AMOUNT\" where amount0_.\"id\"=?");

Question: Why two decimal places though?

There are some other problems

  1. cast as Long in HQL - int8 in SQL (but long is bigger than int8)
  2. cast as Double in HQL - float8 in SQL (but double is more precise than float8)

Using:

  1. Hibernate-core-jakarta 5.6.15.Final
  2. Postgres jdbc 42.7.4

Upvotes: 3

Views: 48

Answers (1)

Lajos Arpad
Lajos Arpad

Reputation: 76943

A similar question was asked here. The solution boiled down to something like

    @Override
    public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
        super.contributeTypes(typeContributions, serviceRegistry);
        typeContributions.getTypeConfiguration().getDdlTypeRegistry().addDescriptor(
            new DdlTypeImpl(SqlTypes.NUMERIC, "numeric", this)
        );

and casting to BigDecimal. The reasoning, as Beikov presented was

You can register a custom DdlType into DdlTypeRegistry in a TypeContributor for the NUMERIC/DECIMAL type code. The custom DdlType can then override getCastTypeName to return just numeric if length and precision are null.

This would be a nice improvement for Hibernate ORM though, so if you could create a Jira improvement ticket in our issue tracker and possibly also provide a PR with the improvement, chances are good that we will accept this

Upvotes: 0

Related Questions