3

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
2
  • Good question, but as a note that support for CAST() is up to the JDBC driver. Why do you need to do this explicit cast? Commented Dec 8, 2024 at 9:54
  • @TimBiegeleisen I do not think the jdbc-driver is responsible for two digits precision. The SQL is generated by hibernate. The hibernate dialect might more likely have an impact to the SQL generation. Commented Dec 8, 2024 at 19:41

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.