views:

3453

answers:

3

Hello friends,

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.

A: 

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.

Aaron Digulla
yes.. following is my dialect<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
amar4kintu
In that case, the type of your ID column is the problem. See my edits.
Aaron Digulla
+4  A: 

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>
ChssPly76
Hello ChssPly76,Thanks for your reply.I converted my hibernate to use long wherever there is BigDecimal to use sequence in hibernate. It solves my problem.I posted it here to know how can I extend hibernate identifiergenerator class to use bigDecimal for sequence generator.I am not getting it exactly. But converting BigDecimal to long solves problem for sure.Thanks.
amar4kintu
I've explained how to extend `SequenceGenerator` in my answer above. Take a look at its `generate()` method - you basically would copy the entire thing and replace `IdentifierGeneratorFactory.get()` with `resultSet.get()` to obtain your `BigDecimal` value.
ChssPly76
I set all my big_decimal fields to be considered as long in my hibernate generation file.. so it works fine now.. thanks.. for your support..
amar4kintu
A: 

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. -Yuv

Yuvaraj V