本文介绍了Oracle休眠序列生成器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Oracle 11g,Java(struts2)和Hibernate开发了一个应用程序。

我有一个名为mytemp的表mytemp_id,它的类型是NUMBER(22, 0)。

在我的mytemp.hbm.xml文件中,id如下:

 < id name =mytempIdtype =big_decimal> 
< column name =MYTEMP_IDprecision =22scale =0/>
< generator class =sequence>
< param name =sequence> MYTEMP_TEMP_ID_SEQ< / param>
< / generator>
< / id>

在我的Oracle数据库中,名为MYTEMP_TEMP_ID_SEQ的序列在Oracle中创建并正常工作。



现在当我尝试使用hibernate插入记录时,它给了我以下错误



org.hibernate.id。 IdentifierGenerationException:这个id生成器生成long,integer,short或string

看起来我的序列返回Number,hibenate认为它是BigDecimal,而hibernate的sequece生成器类只考虑long,integer,short和string的值。

Hibernate应该没有BigDecimal的问题。但我认为他们还没有为序列生成器实现BigDecimal



任何人都可以帮我解决问题吗?

谢谢。

解决方案

老实说,我无法想象为什么你会坚持让你的ID为BigDecimal而不是长。最大长期价值是 9,223,372,036,854,775,807 ,尽管这个数字大概是NUMBER(22)的最大值的千分之一,但确实应该足够。如果您要生成一个百万标识符每秒,则必须在 300,000年的情况下执行此操作以耗尽序列。也就是说,为了让您的标识符生成为BigDecimal,您需要编写自己的生成器。您可以通过扩展Hibernate的内置SequenceGenerator并覆盖它的 generate()方法来实现。 IdentifierGeneratorFactory.get()只支持long / int / short / String,您可以从结果集中获取序列值BigDecimal。



然后您需要类= com.mypackage.BigDecimalGenerator >
< param name =sequence> MYTEMP_TEMP_ID_SEQ< / param>
< / generator>


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.

解决方案

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>

这篇关于Oracle休眠序列生成器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:51