Oracle Hibernate 序列生成器问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1276839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle hibernate sequence generator problem
提问by amar4kintu
I am developing an application using oracle 11g, Java(struts2) and Hibernate.
我正在使用 oracle 11g、Java(struts2) 和 Hibernate 开发应用程序。
I have table named mytemp with column mytemp_id which is of type NUMBER(22,0).
我有一个名为 mytemp 的表,其中 mytemp_id 列的类型为 NUMBER(22,0)。
In my mytemp.hbm.xml file id is as given below
在我的 mytemp.hbm.xml 文件中,id 如下所示
<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.
在我的名为“MYTEMP_TEMP_ID_SEQ”的 Oracle 数据库序列中创建并在 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
org.hibernate.id.IdentifierGenerationException:这个 id 生成器生成长、整数、短或字符串
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.
似乎当我的序列返回 Number 时,hibernate 将其视为 BigDecimal,而 hibernate 的序列生成器类仅考虑长、整数、短和字符串的值。
Hibernate should not have problem with BigDecimal. But I think they have not implemented BigDecimal for sequence generator
Hibernate 应该没有 BigDecimal 的问题。但我认为他们没有为序列生成器实现 BigDecimal
Can any one help me solving the problem?
任何人都可以帮助我解决问题吗?
Thanks.
谢谢。
回答by ChssPly76
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 millionidentifiers every second, you would have to do that for 300,000 yearsin order to exhaust your sequence.
老实说,我无法想象为什么您会坚持将 ID 设为 BigDecimal 而不是 long。最大 long 值是9,223,372,036,854,775,807
虽然不可否认大约是最大 NUMBER(22) 值的千分之一,但确实应该足够了。如果您每秒生成 100万个标识符,您将不得不这样做300,000 年才能耗尽您的序列。
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.
也就是说,为了将您的标识符生成为 BigDecimal,您需要编写自己的生成器。您可以通过扩展 Hibernate 的内置 SequenceGenerator 并覆盖其generate()
方法来做到这一点。与其调用IdentifierGeneratorFactory.get()
只支持 long / int / short / String 的方法,不如从结果集中获取序列值作为 BigDecimal。
You will then need to declare your generatorby specifying its full class name:
<generator class="com.mypackage.BigDecimalGenerator">
<param name="sequence">MYTEMP_TEMP_ID_SEQ</param>
</generator>
回答by Aaron Digulla
Did you set the correct Dialect? That should be enough to make Hibernate understand the result of the sequence.
您是否设置了正确的方言?这应该足以让 Hibernate 理解序列的结果。
[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.
[编辑] 问题是您的序列类型与您的列类型不匹配。序列(根据 Hibernate 的错误消息)可以转换为 long、integer、short 或 string,而您的序列返回 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.
我建议将 ID 列的类型指定为“long”,即使 Oracle 不知道该类型。在内部,Hibernate 应该能够正确地为每个人投射一切。
回答by Yuvaraj V
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 integertype or to have control over the integertype values for some reason(might be project specific).
确实。考虑到它可以生成的唯一记录的数量,长 id 必须足够。仅具有特殊值的特定生成器可能不是整数类型或出于某种原因控制整数类型值(可能是项目特定的)。
Also, generator is database specific like sequence for oracle, so dialact definition does matter also.
此外,生成器是特定于数据库的,就像 oracle 的序列一样,所以方言定义也很重要。