Java 使用多个数据库模式的 JPA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1301399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA using multiple database schemas
提问by javacoder
I'm having a bit of trouble with one particular issue using JPA/Spring:
我在使用 JPA/Spring 时遇到了一个特定问题:
How can I dynamically assign a schema to an entity?
如何将架构动态分配给实体?
We have TABLE1 that belongs to schema AD and TABLE2 that is under BD.
我们有属于模式 AD 的 TABLE1 和属于 BD 的 TABLE2。
@Entity
@Table(name = "TABLE1", schema="S1D")
...
@Entity
@Table(name = "TABLE2", schema="S2D")
...
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)
模式可能不会硬编码在注释属性中,因为它取决于环境 (Dev/Acc/Prd)。(接受模式是 S1A 和 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?
以便根据驻留在环境中的属性文件替换模式?
Cheers
干杯
采纳答案by carmen_munich
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.xml解决了这个问题,我在声明db shema时引用了所需的orm.xml文件
<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>
now you can create a EntityManagerFactory for your special schema
现在您可以为您的特殊架构创建一个 EntityManagerFactory
EntityManagerFactory emf = Persistence.createEntityManagerFactory("schemaOne");
回答by DataNucleus
One thing you can do if you know at deployment is to have 2 orm.xml files. One for schema1 and one for schema2 and then in the persistence.xml you have 2 persistence-units defined. Putting annotations is an anti-pattern if needing to change things like schema
如果您知道在部署时可以做的一件事是拥有 2 个 orm.xml 文件。一个用于 schema1,一个用于 schema2,然后在 persistence.xml 中定义了 2 个持久性单元。如果需要更改架构等内容,则放置注释是一种反模式
回答by Nico
Annotation arguments have to be final and can therefore not be changed at runtime.
注释参数必须是最终的,因此不能在运行时更改。
回答by Dominik
You could have two DataSource declarations (one for each schema) in your context.xml and define two persistence units using this datasources. The context.xml can then be different on the appservers of the different environments.
您可以在 context.xml 中有两个 DataSource 声明(每个架构一个),并使用此数据源定义两个持久性单元。然后,不同环境的应用服务器上的 context.xml 可能会有所不同。
回答by user3092746
Try following:
尝试以下操作:
puplic class MyClass {
public static final String S1D="S1D";
public static final String S2D="S2D";
}
@Entity
@Table(name = "TABLE1", schema=MyClass.S1D)
...
@Entity
@Table(name = "TABLE2", schema=MyClass.S2D)
...
回答by Kalpesh Soni
when you create a datasource, you may be able to initialize the connection to use different schema
创建数据源时,您可以初始化连接以使用不同的架构
e.g. for weblogic
例如对于网络逻辑