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

标签 java oracle jpa persistence

出于安全原因,我们的 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 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 并使用 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/

相关文章:

java - 如何在 Neo4j Embedded 中查找具有标签子集的节点

sql - 相当于 SQL Server SCHEMA 的 Oracle

java - JPA 标准多对多加入还是不加入?

java - 为什么 Map.compute() 采用 BiFunction

java - java 中的堆空间要求 - Arrays.sort() 与 Collections.sort()

java - 事务和 JPA/Persistence.xml

java - 方法 : java. lang.String.getCatalogName() 的签名不适用于参数

sql - 在遗留数据库上实现乐观并发

Java新手: JPA and EJB workflow question

postgresql - PicketLink: 'ERROR: invalid byte sequence for encoding "UTF 8": 0x00' 设置密码时