2017-04-12 95 views
0

我已经阅读了一些答案,如追加?searchpath = myschema或?currentSchema = myschema,但它仍然不起作用在我的情况。我使用NetBeans,并且可以执行命令到与预期模式的连接,它运行良好,但在运行时,Glassfish只连接到公共模式,忽略?currentSchema = myschema。我的PostgreSQL的版本是9.6和JDBC驱动程序的版本是最新的42.0.0在glassfish jdbc连接池中指定模式?

这是我与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> 
+0

架构不是连接字符串的一部分。尝试将它追加到关系名称上? –

+0

@VaoTsun [PostgreSQL JDBC文档](https://jdbc.postgresql.org/documentation/94/connect。html#connection-parameters)会有所不同:_“'currentSchema = String'指定要在搜索路径中设置的模式,该模式将用于解析通过此连接的语句中使用的非限定对象名称。”_ –

+0

@ Vao Tsun,将其添加到关系名称?你的意思是实体类中的实体还是表注释属性?但我想要一个类在多个模式中使用。 – Mideel

回答

1

我在结合Google的一些结果后,我终于明白了自己的想法,我希望这对于像我这样的人有用。这是我做出的一些结论。

  1. 添加在JDBC-conncetion池元素currentSchema = MYSCHEMA如果连接在JPA使用,并指定在持久化单元,就像我前面的例子中JPA不会改变任何东西:

    的java :app/myjndisource

    EntityManager仍然使用公共模式并忽略指定的模式。

  2. 我们需要使用orm.xml文件在这个例子中指定我们想要的模式,如:

    JPA - EclipseLink - How to change default schema

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

    网页 - > WEB-INF - >类 - > META-INF - >(映射文件)

Netbeans web application structure

然后在持久性单元,我们可以指定映射文件元素,但请记住这2点

答:在Netbeans(或其他IDES)中,必须将映射文件元素放置在jta-data-source元素之后但在类元素之前,否则会出现一些部署错误在此链接中:

https://netbeans.org/bugzilla/show_bug.cgi?id=170348

B.如果你的名字名为orm.xml中的映射文件,并将其包含在您的持久性单元相同的目录,自动持久性单元包括映射文件时,即使不指定它与映射文件元素。

因此,如果您有2个映射文件,其中一个名为orm.xml并且它们中的一个包含模式元素,并且您的持久性单元对其他映射文件使用映射文件元素,则会因为存在冲突的模式而出现错误。