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
How do you set the schema name for sequences at deploy time when using JPA?
提问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
@SequenceGenerator
annotation and the equivalentsequence-generator
element do allow to specify aschema
(andcatalog
) name. - the
schema
subelement 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 schema
subelement 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
schema
subelement applies to allentities, tables, secondary tables, join tables, collection tables, table generators, and sequence generatorsin the persistence unit.The
schema
subelement is overridden by anyschema
subelement of theentity-mappings
element; anyschema
element explicitly specified in theTable
orSecondaryTable
annotation on an entity or anyschema
attribute on anytable
orsecondary-table
subelement defined within anentity
element; anyschema
element explicitly specified in aTableGenerator
annotation ortable-generator
subelement; any schema element explicitly specified in aSequenceGenerator
annotation orsequence-generator
subelement; anyschema
element explicitly specified in aJoinTable
annotation orjoin-table
subelement; and any schema element explicitly specified in aCollectionTable
annotation orcollection-table
subelement.
12.2.1.1 架构
该
schema
子元素适用于所有实体,表,二次表,连接表,采集表,表发生器和 序列生成的持久性单元。的
schema
子元件是由任何覆盖schema
所述的子元素entity-mappings
元件; 实体schema
的Table
或SecondaryTable
注释中明确指定的任何元素 或 元素内定义的schema
任何table
或secondary-table
子元素的任何属性entity
;schema
在TableGenerator
注释或table-generator
子元素中明确指定的 任何元素;在SequenceGenerator
注释或sequence-generator
子元素中明确指定的任何模式元素;schema
在JoinTable
注释或join-table
子元素中明确指定的任何 元素;以及在CollectionTable
注释或collection-table
子元素中明确指定的任何架构元素。
From the JPA 1.0 specification:
来自 JPA 1.0 规范:
10.1.1.1 schema
The
schema
subelement appliesto all entities, table generators, and join tables in the persistence unit.The
schema
subelement is overridden by anyschema
subelement of theentity-mappings
element; anyschema
element explicitly specified in theTable
orSecondaryTable
annotation on an entity or any schema attribute on anytable
orsecondary-table
subelement defined within anentity
element; anyschema
element explicitly specified in aTableGenerator
annotation ortable-generator
subelement; and anyschema
element explicitly specified in aJoinTable
annotation orjoin-table
subelement.
10.1.1.1 架构
该
schema
子元素适用于所有实体,台发电机组,并加入了持久化单元表。的
schema
子元件是由任何覆盖schema
所述的子元素entity-mappings
元件; 实体schema
的Table
或SecondaryTable
注释中明确指定的任何 元素table
或secondary-table
元素内定义的任何或 子元素的任何架构属性entity
;schema
在TableGenerator
注释或table-generator
子元素中明确指定的任何 元素;以及schema
在JoinTable
注释或join-table
子元素中明确指定的任何 元素。
So, unless your provider offers some specific extensions, my suggestions are:
因此,除非您的提供商提供一些特定的扩展,否则我的建议是:
- Upgrade to JPA 2.0 if possible and using the
schema
subelement will do the trick ~or~ - Use a
TableGenerator
if 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?
请原谅我提出了显而易见的问题,但您已将序列的选择权限授予试图从中进行选择的用户,对吗?