JPA使用多个数据库模式 [英] JPA using multiple database schemas

查看:105
本文介绍了JPA使用多个数据库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JPA / Spring时遇到一个特定问题时遇到了一些麻烦:

I'm having a bit of trouble with one particular issue using JPA/Spring:

如何动态地将模式分配给实体?

How can I dynamically assign a schema to an entity?

我们有TABLE1属于模式AD,TABLE2属于BD。

We have TABLE1 that belongs to schema AD and TABLE2 that is under BD.

@Entity
@Table(name = "TABLE1", schema="S1D")
...

@Entity
@Table(name = "TABLE2", schema="S2D")
...

模式可能不是硬编码的注释属性,因为它取决于环境(Dev / Acc / Prd)。 (接受模式是S1A和S2A)

The schemas may not be hardcoded in an annotation attribute as it depends on the environment (Dev/Acc/Prd). (In acceptance the schemas are S1A and S2A)

我如何实现这一目标?是否可以指定某种类型的占位符:

How can I achieve this? Is it possible to specify some kind of placeholders like this:

@Entity
@Table(name = "TABLE1", schema="${schema1}")
...

@Entity
@Table(name = "TABLE2", schema="${schema2}")
...

以便根据驻留在环境中的属性文件替换模式?

so that schemas are replaced based on a property file residing in the environment?

干杯

推荐答案

我有同样的问题我解决了这个问题一个persistence.xml,其中我引用了所需的orm.xml文件,我声明了db shema

I had the same problem I solved that with a persistence.xml in which I refer to the needed orm.xml files within I declared the db shema

<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" >
<persistence-unit name="schemaOne">
    . . .
    <mapping-file>ormOne.xml</mapping-file>
    . . .
</persistence-unit>

<persistence-unit name="schemaTwo">
    . . .
    <mapping-file>ormTwo.xml</mapping-file>
    . . .
 </persistence-unit>
</persistence>

现在你可以为你的特殊架构创建一个EntityManagerFactory

now you can create a EntityManagerFactory for your special schema

EntityManagerFactory emf = Persistence.createEntityManagerFactory("schemaOne");

这篇关于JPA使用多个数据库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆