2017-04-12 91 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並且它們中的一個包含模式元素,並且您的持久性單元對其他映射文件使用映射文件元素,則會因爲存在衝突的模式而出現錯誤。