Hibernate:使用增量和 Oracle Schema 的 ID 生成器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1566263/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:12:30  来源:igfitidea点击:

Hibernate: ID generator using increment and Oracle Schema

oraclehibernate

提问by Krishna

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'

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

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

我在 cfg.xml 中设置了如下默认架构,因为我需要在我的应用程序中使用 OTHER_SCHEMA 中的表。

<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:

在上述情况下,它似乎是一个休眠错误,因为使用 TestTable 对象读取工作正常并正确使用“TEST”模式,但'<generator class="increment"></generator>'不使用“TEST”模式而是使用默认“OTHER_SCHEMA”来获取最大值ID。为最大 ID 生成的查询如下所示:

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.

我无法为生成器指定架构,并且它没有使用我希望它使用的类的 schema="TEST" 属性。

How can this issue be resolved?

如何解决这个问题?

回答by ChssPly76

You canspecify schema for your generator using schemaparameter:

可以使用schema参数为生成器指定架构:

<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 javadocin order to find that out.

遗憾的是,这在 Hibernate 文档中没有很好地描述;您必须查看API javadoc才能找到答案

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

也就是说,马克关于“增量”效率不高的观点是正确的——它在集群环境中也不安全。

回答by user151019

If the hibernate default schema is OTHER_SCHEMA then if you do not supply a schema for a table then it will automatically add the default

如果休眠默认架构是 OTHER_SCHEMA 则如果您不为表提供架构,那么它将自动添加默认架构

Solution is you have to add a schema for all table names except one the default. In this case you might be better off if you set the hibernate default to your schema.

解决方案是您必须为所有表名添加一个模式,默认除外。在这种情况下,如果您将休眠默认设置为您的架构,您可能会更好。

Looking at the sequence using max(id) like that is not usually efficient, especially in Oracle which has sequences exactly for this use.

像这样查看使用 max(id) 的序列通常效率不高,尤其是在 Oracle 中,它的序列正好用于此用途。