出于安全原因,我们的 oracle 数据库对象通常属于与登录用户不同的模式。例如。表在 xx_core 中,我们登录的用户是 xx_app_yy 。
在我的 persistence.xml 中,我定义了一个 orm 文件,以便我可以在部署时指定架构名称,例如:
<mapping-file>xx_schema_orm.xml</mapping-file>
然后在 xx_schema_orm.xml 中我可以定义对象拥有模式,例如:
<persistence-unit-metadata>
<persistence-unit-defaults>
<schema>xx_core</schema>
</persistence-unit-defaults>
</persistence-unit-metadata>
这对表格非常有效,但我找不到序列的等效项。它尝试使用没有模式名称的序列,然后我得到一个异常:
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.:
<sequence-generator name="xx_SEQ_GEN"
sequence-name="xx_core.xx_SEQ"/>
我可能会尝试的解决方法:
- 为用户模式中的序列创建一个数据库 SYNONYM。
- 停止使用序列并使用其他方式生成 ID。
最佳答案
在 JPA 2.0 中:
@SequenceGenerator
注释和等效的sequence-generator
元素 允许指定schema
(和catalog
) 名称。schema
子元素也应该被序列生成器接受。
但这不适用于 JPA 1.0。
我将只引用关于 schema
子元素的部分来说明差异(其他相关部分在下面的引用资料中提到)。来自 JPA 2.0 规范:
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.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.
来自 JPA 1.0 规范:
10.1.1.1 schema
The
schema
subelement applies to 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.
因此,除非您的提供商提供某些特定的扩展,否则我的建议是:
- 如果可能,请升级到 JPA 2.0 并使用
schema
子元素来解决问题 ~or~ - 如果您必须坚持使用 JPA 1.0,请使用
TableGenerator
~或~ - 如果可能,请使用别名(我不知道)。
引用资料
- JPA 1.0 规范
- 第 9.1.37 节“SequenceGenerator 注释”
- 第 10.1.1.1 节“模式”
- 第 12.2.2.5 节“序列生成器”
- JPA 2.0 规范
- 第 11.1.44 节“SequenceGenerator 注释”
- 第 12.2.1.1 节“模式”
- 第 12.2.2.5 节“序列生成器”
关于java - 使用 JPA 时如何在部署时设置序列的模式名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3937986/