JPA、EclipseLink、PostgreSQL 和模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13476679/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:34:09  来源:igfitidea点击:

JPA, EclipseLink, PostgreSQL and schemas

postgresqljpaschemaeclipselink

提问by Olivier J.

I want to develop a Java EE application with JPA (EclipseLink implementation) and PostgreSQL as database.

我想开发一个使用 JPA(EclipseLink 实现)和 PostgreSQL 作为数据库的 Java EE 应用程序。

I chose to have one database and multiples schemas instead of having multiples databases and one schema per database.

我选择了一个数据库和多个模式,而不是每个数据库有多个数据库和一个模式。

So, in my persistence.xml I have something like that :

所以,在我的persistence.xml 中,我有类似的东西:

<persistence-unit name="00" transaction-type="JTA">

    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>todo</jta-data-source>

    <class>...</class>
    <class>...</class>

    <properties>
        <property name="javax.persistence.target-database" value="PostgreSQL" />
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb;create=true" />
        <property name="javax.persistence.jdbc.user" value="user" />
        <property name="javax.persistence.jdbc.password" value="userpwd" />
    </properties>

</persistence-unit>

<persistence-unit name="01" transaction-type="JTA">

    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>todo</jta-data-source>

    <class>...</class>
    <class>...</class>

    <properties>
        <property name="javax.persistence.target-database" value="PostgreSQL" />
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb;create=true" />
        <property name="javax.persistence.jdbc.user" value="user" />
        <property name="javax.persistence.jdbc.password" value="userpwd" />
    </properties>

</persistence-unit>

I can easily reference persistence unit :

我可以轻松引用持久性单元:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("01");
EntityManager em = emf.createEntityManager();

but how can I store information about schemas in persistence.xml ? I want to access schema 00or 01in testdbdatabase.

但是如何在persistence.xml 中存储有关模式的信息?我想访问模式0001testdb数据库中。

I probably could change schema via native SQL directly but is there a way to bind schema to persistence unit ?

我可能可以直接通过本机 SQL 更改架构,但是有没有办法将架构绑定到持久性单元?

Thank you

谢谢

回答by Chris

This same question was asked and answered here: JPA - EclipseLink - How to change default schema

在这里提出并回答了同样的问题: JPA - EclipseLink - How to change default schema

You can override the schema for the entire persistence unit using an orm.xml file, or define it within each table annotation or xml element as needed.

您可以使用 orm.xml 文件覆盖整个持久性单元的架构,或者根据需要在每个表注释或 xml 元素中定义它。