Postgresql 在glassfish jdbc连接池中指定架构?

Postgresql 在glassfish jdbc连接池中指定架构?,postgresql,jpa,jdbc,glassfish,multi-tenant,Postgresql,Jpa,Jdbc,Glassfish,Multi Tenant,我已经阅读了一些答案,比如添加?searchpath=myschema或?currentSchema=myschema,但在我的案例中仍然不起作用。我使用NetBeans,可以执行命令来连接预期的模式,它工作得很好,但在运行时,Glassfish只连接到公共模式,而忽略了?currentSchema=myschema。我的postgresql版本是9.6,JDBC驱动程序版本是最新的42.0.0 这是我的glassfish-resource.xml: <resources>

我已经阅读了一些答案,比如添加?searchpath=myschema或?currentSchema=myschema,但在我的案例中仍然不起作用。我使用NetBeans,可以执行命令来连接预期的模式,它工作得很好,但在运行时,Glassfish只连接到公共模式,而忽略了?currentSchema=myschema。我的postgresql版本是9.6,JDBC驱动程序版本是最新的42.0.0

这是我的glassfish-resource.xml:

<resources>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.postgresql.ds.PGSimpleDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="post-gre-sql_aegwyncreds_dbexerphi_dbaPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="5432"/>
        <property name="databaseName" value="mydb"/>
        <property name="User" value="user"/>
        <property name="Password" value="pass"/>
        <property name="URL" value="jdbc:postgresql://localhost:5432/mydb?currentSchema=myschema"/>
        <property name="driverClass" value="org.postgresql.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="java:app/myjndisource" object-type="user" pool-name="post-gre-sql_mydb_user_dbaPool"/>
</resources>
这是我的持久性单元:

<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="myPU" transaction-type="JTA">
    <jta-data-source>java:app/myjndisource</jta-data-source>
    <class>myclass</class>
    . . . . . . .  . . .
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
    </properties>
  </persistence-unit>
</persistence>

在结合了一些谷歌搜索结果后,我终于自己找到了答案,我希望这对像我一样的人有用。以下是我得出的一些结论

在jdbc连接池元素中添加currentSchema=myschema不会改变JPA中的任何内容,如果该连接在JPA中使用并在持久性单元中指定,就像我前面的示例:

java:app/myjndisource

EntityManager仍然使用公共架构并忽略指定的架构

我们需要使用orm.xml文件来指定我们想要的模式,如本例所示:

可以使用任何名称设置orm.xml文件,但它必须位于类路径上。在NetBeans中,您可以使用此文件夹结构作为示例,因为无法使用NetBeans内置帮助生成此文件

网页->Web-INF->类->META-INF->映射文件

然后在持久性单元中,我们可以指定mappingfile元素,但请记住这两点

A.在Netbeans或其他IDE中,必须将mapping file元素放在jta数据源元素之后,但必须放在class元素之前,否则会出现一些部署错误,如此链接中所示:

B.如果您将映射文件命名为orm.xml,并将其包含在与持久化单元相同的目录中,则持久化单元将自动包含该映射文件,尽管您没有使用mapping file元素指定它


因此,如果您有两个映射文件,其中一个名为orm.xml,并且两个文件都包含schema元素,并且您的持久化单元对另一个映射文件使用mapping file元素,则会出现错误,因为存在冲突的架构。

架构不是连接字符串的一部分。请尝试将其附加到关系名称?。@VaoTsun begs to different:currentSchema=String指定要在搜索路径中设置的架构。此架构将用于解析通过此连接的语句中使用的非限定对象名。@Vao Tsun,是否将其附加到关系名?您的意思是在实体类中添加实体或表注释属性吗?但我希望在多个模式中使用一个类。可能是因为JPA,没有JPA的正常JDBC命令可能会工作,但即使我们使用公共模式,JPA仍然使用公共模式?可能JPA从public.mytable生成sql命令,如SELECT*之类的?