在使用JPA时,如何在部署时为序列设置模式名称? [英] How do you set the schema name for sequences at deploy time when using JPA?
问题描述
出于安全原因,我们的oracle db对象通常属于与登录用户不同的架构。例如。表位于xx_core中,我们登录的用户是xx_app_yy。
在我的persistence.xml中,我定义了一个orm文件,以便我可以在部署时指定模式名称,例如:
< mapping-file> xx_schema_orm.xml< / mapping -file>
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>
然后在xx_schema_orm.xml中我可以定义对象拥有模式,例如:
Then in the xx_schema_orm.xml I can define the object-owning-schema eg.:
<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)
我试图将模式名称设置为xx_schema_orm.xml中生成器的序列名称,但无法使其工作,例如:
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.:
<sequence-generator name="xx_SEQ_GEN"
sequence-name="xx_core.xx_SEQ"/>
我可以尝试的解决方法:
Workarounds I may try:
- 为用户架构中的序列创建数据库SYNONYM。
- 停止使用序列并使用其他方式生成ID。
推荐答案
在JPA 2.0中:
-
@SequenceGenerator
注释和等效的序列生成器
元素允许指定schema
(和catalog
)name 。 -
架构
子元素也应遵循序列生成器。
- the
@SequenceGenerator
annotation and the equivalentsequence-generator
element do allow to specify aschema
(andcatalog
) name. - the
schema
subelement should be honored by sequence generators as well.
但这不适用于JPA 1.0。
But this doesn't apply to JPA 1.0.
我将引用有关架构
子元素的部分来说明差异(其他相关部分在以下参考资料)。从JPA 2.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:
12.2.1.1架构
架构
子元素适用于所有实体,表,辅助
表,连接表,集合
表,表生成器和$ b持久性单元中的$ b 序列生成器。
12.2.1.1 schema
The
schema
subelement applies to all entities, tables, secondary tables, join tables, collection tables, table generators, and sequence generators in the persistence unit.
架构
子元素被覆盖
entity-mappings
元素的任何 schema
子元素;在
表
或或SecondaryTable中明确指定的任何
实体上的注释架构
元素
或任何表上的任何
或架构
属性
secondary-table
子元素在实体
元素中定义;任何架构
元素
在
TableGenerator
注释或
<$中明确指定c $ c> table-generator 子元素;
a SequenceGenerator
注释或
sequence-generator
中明确指定的任何
架构元素子元素;任何
架构
元素在 JoinTable
注释或
<$中明确指定
c $ c> join-table 子元素;和
a CollectionTable
注释或
collection-table
架构元素> subelement。
The schema
subelement is overridden
by any schema
subelement of the
entity-mappings
element; any schema
element explicitly specified in the
Table
or SecondaryTable
annotation
on an entity or any schema
attribute
on any table
or secondary-table
subelement defined within an entity
element; any schema
element
explicitly specified in a
TableGenerator
annotation or
table-generator
subelement; any
schema element explicitly specified in
a SequenceGenerator
annotation or
sequence-generator
subelement; any
schema
element explicitly specified
in a JoinTable
annotation or
join-table
subelement; and any
schema element explicitly specified in
a CollectionTable
annotation or
collection-table
subelement.
从JPA 1.0规范:
From the JPA 1.0 specification:
10.1.1.1架构
架构
子元素适用于所有
实体,表生成器,并在持久性单元中加入
表。
10.1.1.1 schema
The
schema
subelement applies to all entities, table generators, and join tables in the persistence unit.
模式
子元素被
实体映射$ c的任何
架构
子元素覆盖
$ c>元素;任何
架构
元素在表
或或
子元素;和任何中显式指定
实体上的
注释或任何表上的任何模式
或
属性
secondary-table
子元素在实体
元素中定义
;任何
架构
元素在 TableGenerator
注释或
<$中明确指定
c $ c> table-generator
架构
元素在 JoinTable
注释中显式指定
或
join-table
子元素。
The schema
subelement is overridden
by any schema
subelement of the
entity-mappings
element; any
schema
element explicitly specified
in the Table
or SecondaryTable
annotation on an entity or any schema
attribute on any table
or
secondary-table
subelement defined
within an entity
element; any
schema
element explicitly specified
in a TableGenerator
annotation or
table-generator
subelement; and any
schema
element explicitly specified
in a JoinTable
annotation or
join-table
subelement.
因此,除非您的提供商提供某些特定扩展,我的建议是:
So, unless your provider offers some specific extensions, my suggestions are:
- 如果可能,使用
架构
子元素升级到JPA 2.0如果你不得不坚持使用JPA 1.0~或〜 $,那么就可以了解〜或〜 - 使用
TableGenerator
b $ b - 如果可能,请使用别名(我不知道)。
- 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 1.0规格
- 第9.1.37节SequenceGenerator注释
- 第10.1.1.1节架构
- 第12.2.2.5节序列生成器
- 第11.1.44节SequenceGenerator注释
- 第12.2.1.1节架构
- 第12.2.2.5节序列 - 发电机
这篇关于在使用JPA时,如何在部署时为序列设置模式名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!