Question

I am developing an application using oracle 11g, Java(struts2) and Hibernate.

I have table named mytemp with column mytemp_id which is of type NUMBER(22,0).

In my mytemp.hbm.xml file id is as given below

<id name="mytempId" type="big_decimal">
        <column name="MYTEMP_ID" precision="22" scale="0" />
        <generator class="sequence">
            <param name="sequence">MYTEMP_TEMP_ID_SEQ</param>
        </generator>
    </id>

In my Oracle database sequence named "MYTEMP_TEMP_ID_SEQ" is created and working fine in Oracle.

Now when I try to insert record using hibernate, it gives me following error

org.hibernate.id.IdentifierGenerationException: this id generator generates long, integer, short or string

It seems that as my sequence returns Number, hibenate considering it as BigDecimal, while hibernate's sequece generator class considering values that are long, integer, short and string only.

Hibernate should not have problem with BigDecimal. But I think they have not implemented BigDecimal for sequence generator

Can any one help me solving the problem?

Thanks.

Was it helpful?

Solution

To be honest with you, I can't imagine why you would insist on having your ID as BigDecimal instead of long. Maximum long value is 9,223,372,036,854,775,807 which, although admittedly is about one thousandth of maximum NUMBER(22) value, should really be quite enough. If you were to generate one million identifiers every second, you would have to do that for 300,000 years in order to exhaust your sequence.

That said, in order to have your identifier generated as BigDecimal you will need to write your own generator. You can do that by extending Hibernate's built-in SequenceGenerator and overriding its generate() method. Instead of calling through to IdentifierGeneratorFactory.get() which only supports long / int / short / String you'd obtain your sequence value from result set as BigDecimal.

You will then need to declare your generator by specifying its full class name:

<generator class="com.mypackage.BigDecimalGenerator">
  <param name="sequence">MYTEMP_TEMP_ID_SEQ</param>
</generator>

OTHER TIPS

Did you set the correct Dialect? That should be enough to make Hibernate understand the result of the sequence.

[EDIT] The problem is that the type of your sequence doesn't match the type of your column. The sequence (as per Hibernate's error message) can be cast to long, integer, short or string while your sequence returns a BigDecimal.

I suggest to specify the type of the ID column as "long" even though Oracle doesn't know that type. Internally, Hibernate should then be able to cast everything for everyone correctly.

Definitely. Long id must be enough always considering number of unique records it can generate. The particular generator mere to have special values might be other than integer type or to have control over the integer type values for some reason(might be project specific).

Also, generator is database specific like sequence for oracle, so dialact definition does matter also.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top