Hibernate:使用增量和Oracle模式的ID生成器 [英] Hibernate: ID generator using increment and Oracle Schema

查看:186
本文介绍了Hibernate:使用增量和Oracle模式的ID生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hiberbnate 3.1.3。我有一个映射如下,当我尝试插入一个记录到TEST_TABLE,我得到一个异常:'线程中的异常'主要org.hibernate.exception.SQLGrammarException:无法获取增量生成器的初始值'

 < class name =com.test.app.to.TestTabletable =TEST_TABLEschema =TEST> 
< id name =testIdtype =long>
< column name =TEST_IDprecision =12scale =0/>
< generator class =increment>< / generator>
< / id>
< / class>

我在cfg.xml中设置了如下的默认模式,因为我需要使用OTHER_SCHEMA中的表

 < property name =hibernate.default_schema> OTHER_SCHEMA< / property> 

在上面的例子中,它似乎是一个Hibernate Bug,因为使用TestTable对象的读取工作正常,正确使用'TEST'模式,但'< generator class =increment>< / generator>'不使用'TEST'模式,而是使用用于获取最大ID的默认'OTHER_SCHEMA'。为max ID生成的查询读取如下:

  Hibernate:从OTHER_SCHEMA.TEST_TABLE中选择max(TEST_ID)

我无法为生成器指定模式,也没有使用类的schema =TEST属性我希望它可以使用。



这个问题怎么解决? 您可以使用 schema 参数为您的生成器指定模式:

c $ c>< generator class =increment>
< param name =schema> TEST< / param>
< / generator>

不幸的是,这在Hibernate文档中没有很好的描述。您必须查看 API javadoc 以便找到答案。



也就是说,Mark对于增量的说法并不是非常高效 - 它在集群环境中也不安全。


I am using Hiberbnate 3.1.3. I have a mapping as below and when I try to insert a record into the TEST_TABLE, I get an Exception: 'Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator'

<class name="com.test.app.to.TestTable" table="TEST_TABLE" schema="TEST">
        <id name="testId" type="long">
            <column name="TEST_ID" precision="12" scale="0" />
            <generator class="increment"></generator>
        </id>
</class>

I have default schema set as below in the cfg.xml as I need to use tables from the OTHER_SCHEMA as well in my application.

<property name="hibernate.default_schema">OTHER_SCHEMA</property>

In the above case it seems to be a Hibernate Bug as a read using a TestTable object works fine and uses the 'TEST' schema correctly, but the '<generator class="increment"></generator>' does not use the 'TEST' schema but uses the default 'OTHER_SCHEMA' for getting the max ID. The query generated for max ID reads as below:

Hibernate: select max(TEST_ID) from OTHER_SCHEMA.TEST_TABLE

I am not able to specify a schema for the generator and it is not using the schema="TEST" attribute of the class which I would expect it to use.

How can this issue be resolved?

解决方案

You can specify schema for your generator using schema parameter:

<generator class="increment">
    <param name="schema">TEST</param>
</generator>

Sadly, this is not well described in Hibernate documentation; you'd have to look at the API javadoc in order to find that out.

That said, Mark is right with regards to "increment" not being very efficient - it's also not safe in a cluster environment.

这篇关于Hibernate:使用增量和Oracle模式的ID生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆