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

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

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

这适用于表格,但我找不到序列的等价物.它尝试使用没有模式名称的序列,然后我得到一个例外:

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中生成器的序列名称的一部分,但无法使其工作,例如:

我可以尝试的解决方法:

>为用户架构中的序列创建数据库SYNONYM.
>停止使用序列并使用其他方式生成ID.

最佳答案
在JPA 2.0中:

> @SequenceGenerator注释和等效序列生成器元素允许指定模式(和目录)名称.
>模式子元素也应该受序列生成器的尊重.

但这不适用于JPA 1.0.

我将引用有关模式子元素的部分来说明差异(其他相关部分在下面的参考文献中提到).从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 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规范:

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

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

>如果可能的话升级到JPA 2.0并使用模式子元素可以实现〜或〜
>如果必须坚持使用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节“序列发生器”

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...