oracle 使用 JPA 时,如何在部署时为序列设置模式名称?

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

How do you set the schema name for sequences at deploy time when using JPA?

javaoraclejpapersistence

提问by AmanicA

For security reasons, our oracle db objects normally belong to a different schema than the logged in user. Eg. tables are in xx_core and the user we log in is xx_app_yy . In my persistence.xml I define a orm file so that I can specify the schema name at deploy time eg.: <mapping-file>xx_schema_orm.xml</mapping-file>

出于安全原因,我们的 oracle db 对象通常与登录用户属于不同的模式。例如。表在 xx_core 中,我们登录的用户是 xx_app_yy 。在我的 persistence.xml 中,我定义了一个 orm 文件,以便我可以在部署时指定架构名称,例如: <mapping-file>xx_schema_orm.xml</mapping-file>

Then in the xx_schema_orm.xml I can define the object-owning-schema eg.:

然后在 xx_schema_orm.xml 我可以定义对象拥有模式,例如:

<persistence-unit-metadata>
  <persistence-unit-defaults>
    <schema>xx_core</schema>
  </persistence-unit-defaults>
</persistence-unit-metadata>

This works great for tables, but I can't find the equivalent for sequences. It tries to use the sequence without the schema name and then I get an exception:

这对表格很有用,但我找不到序列的等效项。它尝试使用没有模式名称的序列,然后我得到一个异常:

2010-10-14 03:04:05,423:DEBUG   could not get next sequence value [select xx_SEQ.nextval from dual]     - org.hibernate.util.JDBCExceptionReporter
java.sql.SQLException: ORA-02289: sequence does not exist

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)

I've tried to set the schema name as part of the sequence name for a generator in the xx_schema_orm.xml, but could not get it working eg.:

我试图将模式名称设置为 xx_schema_orm.xml 中生成器的序列名称的一部分,但无法使其正常工作,例如:

<sequence-generator name="xx_SEQ_GEN"
sequence-name="xx_core.xx_SEQ"/>

Workarounds I may try:

我可以尝试的解决方法:

  • make a database SYNONYM for the sequences in the user's schema.
  • stop using sequences and use some other way to generate ids.
  • 为用户模式中的序列创建一个数据库 SYNONYM。
  • 停止使用序列并使用其他方式生成 ID。

采纳答案by Pascal Thivent

In JPA 2.0:

在 JPA 2.0 中:

  • the @SequenceGeneratorannotation and the equivalent sequence-generatorelement do allow to specify a schema(and catalog) name.
  • the schemasubelement should be honored by sequence generatorsas well.
  • @SequenceGenerator注释和等效sequence-generator元素确实允许指定schema(和catalog)的名字
  • schema子元素应该通过序列发生器兑现为好。

But this doesn't apply to JPA 1.0.

但这不适用于 JPA 1.0。

I'll just quote the sections about the schemasubelement to illustrate the differences (other relevant sections are mentioned in the references below). From the JPA 2.0 specification:

我将引用有关schema子元素的部分来说明差异(其他相关部分在下面的参考资料中提到)。从 JPA 2.0 规范:

12.2.1.1 schema

The schemasubelement applies to allentities, tables, secondary tables, join tables, collection tables, table generators, and sequence generatorsin the persistence unit.

The schemasubelement is overridden by any schemasubelement of the entity-mappingselement; any schemaelement explicitly specified in the Tableor SecondaryTableannotation on an entity or any schemaattribute on any tableor secondary-tablesubelement defined within an entityelement; any schemaelement explicitly specified in a TableGeneratorannotation or table-generatorsubelement; any schema element explicitly specified in a SequenceGeneratorannotation or sequence-generatorsubelement; any schemaelement explicitly specified in a JoinTableannotation or join-tablesubelement; and any schema element explicitly specified in a CollectionTableannotation or collection-tablesubelement.

12.2.1.1 架构

schema子元素适用于所有实体,表,二次表,连接表,采集表,表发生器和 序列生成的持久性单元。

schema子元件是由任何覆盖schema所述的子元素 entity-mappings元件; 实体schemaTableSecondaryTable注释中明确指定的任何元素 或 元素内定义的schema任何tablesecondary-table子元素的任何属性entityschemaTableGenerator注释或 table-generator子元素中明确指定的 任何元素;在SequenceGenerator注释或 sequence-generator子元素中明确指定的任何模式元素;schemaJoinTable注释或 join-table子元素中明确指定的任何 元素;以及在CollectionTable注释或 collection-table子元素中明确指定的任何架构元素。

From the JPA 1.0 specification:

来自 JPA 1.0 规范:

10.1.1.1 schema

The schemasubelement appliesto all entities, table generators, and join tables in the persistence unit.

The schemasubelement is overridden by any schemasubelement of the entity-mappingselement; any schemaelement explicitly specified in the Tableor SecondaryTableannotation on an entity or any schema attribute on any tableor secondary-tablesubelement defined within an entityelement; any schemaelement explicitly specified in a TableGeneratorannotation or table-generatorsubelement; and any schemaelement explicitly specified in a JoinTableannotation or join-tablesubelement.

10.1.1.1 架构

schema子元素适用于所有实体,台发电机组,并加入了持久化单元表。

schema子元件是由任何覆盖schema所述的子元素 entity-mappings元件; 实体schemaTableSecondaryTable注释中明确指定的任何 元素tablesecondary-table元素内定义的任何或 子元素的任何架构属性entityschemaTableGenerator注释或 table-generator子元素中明确指定的任何 元素;以及schemaJoinTable注释或 join-table子元素中明确指定的任何 元素。

So, unless your provider offers some specific extensions, my suggestions are:

因此,除非您的提供商提供一些特定的扩展,否则我的建议是:

  • Upgrade to JPA 2.0 if possible and using the schemasubelement will do the trick ~or~
  • Use a TableGeneratorif you have to stick with JPA 1.0 ~or~
  • Use an alias if this is possible (I don't know).
  • 如果可能,升级到 JPA 2.0 并使用schema子元素就可以了~或者~
  • TableGenerator如果您必须坚持使用 JPA 1.0,请使用 a ~或~
  • 如果可能,请使用别名(我不知道)。

References

参考

  • JPA 1.0 Specification
    • Section 9.1.37 "SequenceGenerator Annotation"
    • Section 10.1.1.1 "schema"
    • Section 12.2.2.5 "sequence-generator"
  • JPA 2.0 Specification
    • Section 11.1.44 "SequenceGenerator Annotation"
    • Section 12.2.1.1 "schema"
    • Section 12.2.2.5 "sequence-generator"
  • JPA 1.0 规范
    • 第 9.1.37 节“序列生成器注释”
    • 第 10.1.1.1 节“架构”
    • 第 12.2.2.5 节“序列生成器”
  • JPA 2.0 规范
    • 第 11.1.44 节“序列生成器注释”
    • 第 12.2.1.1 节“架构”
    • 第 12.2.2.5 节“序列生成器”

回答by DCookie

Forgive me for asking the obvious, but you have granted select privilege on the sequences to the user that's trying to select from them, right?

请原谅我提出了显而易见的问题,但您已将序列的选择权限授予试图从中进行选择的用户,对吗?