简体   繁体   English

JPA变量持久性单元

[英]JPA variable persistence units

I'm trying to develop a website using java EE, which will be deployed to a remote server, i am trying to implement JPA into the application. 我正在尝试使用Java EE开发一个网站,该网站将部署到远程服务器,我正在尝试在应用程序中实现JPA。
For testing purposes i'd like to create a variable persistence unit, so that on the local deployment, the application will use my local mySQL server, while on the remote deployment it will use the server's provided mySQL server. 为了进行测试,我想创建一个变量持久性单元,以便在本地部署中,应用程序将使用我的本地mySQL服务器,而在远程部署中,它将使用服务器提供的mySQL服务器。

the problem however is that i'm running on glassfish locally, and jboss remote, so i can't make the resource JNDI names for the datasources the same (since jboss requires "java:/" or "java:jboss/" as a prefix, while glassfish doesn't allow :'s in JNDI names) 但是问题是我在本地的glassfish上运行,并且jboss远程运行,因此我无法使数据源的资源JNDI名称相同(因为jboss需要将“ java:/”或“ java:jboss /”作为前缀,而glassfish不允许使用:的JNDI名称)

another problem is that i'm not simply allowed to create 2 persistence units with the same name, 另一个问题是,我不能简单地创建两个具有相同名称的持久性单元,
i've tried making 2 different persistence units, but then the deployment fails because one of the persistence units fails to resolve. 我尝试制作2个不同的持久性单元,但是部署失败,因为其中一个持久性单元无法解析。

below is my persistence.xml at this time: 以下是我目前的persistence.xml:

<persistence-unit name="LocalPU">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/website</jta-data-source>
    <properties>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        <property name="eclipselink.ddl-generation.output-mode" value="both"/>
    </properties>
        </persistence-unit>

<persistence-unit name="RemotePU">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:/website</jta-data-source>
    <properties>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        <property name="eclipselink.ddl-generation.output-mode" value="both"/>
    </properties>
</persistence-unit>

so my question is, is it possible to have an entitymanager resolve to EITHER of these persistence units, but not require both persistence units to be available 所以我的问题是,是否有一个实体管理器可以解决这些持久性单元中的任何一个,但是不要求两个持久性单元都可用

EDIT: 编辑:

about 5 minutes after posting this question, i found an article that suggests using environment variables 发布此问题大约5分钟后,我发现了一篇建议使用环境变量的文章

however this does not seem to work within glassfish, 但是这似乎不适用于玻璃鱼,

this persistence.xml: 这个persistence.xml:

<persistence-unit name="LocalPU">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>${myds}</jta-data-source>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="both"/>
        </properties>
</persistence-unit>

and the JVM-option -Dmyds=jndi/website results in the following error: 并且JVM选项-Dmyds=jndi/website导致以下错误:

Exception while preparing the app : Invalid resource : ${myds}__pm
com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : ${myds}__pm

which leads me to believe environment variables can't be parsed within glassfish (???) 这使我相信在玻璃鱼中无法解析环境变量(???)

after following the tips provided in the first comment above, i have concluded that the question is irrelevant, and that my issues were caused by my misunderstanding of how JNDI names are displayed/parsed differently between glassfish, jboss, and JPA (in my case this has switched to hibernate, since openshift's Jboss servers turned out not to support eclipselink after all) 在遵循了上面第一条评论中提供的技巧之后,我得出结论,这个问题是不相关的,并且我的问题是由于我对在glassfish,jboss和JPA之间如何显示/解析JNDI名称的误解引起的(在我的情况下,已切换为休眠状态,因为openshift的Jboss服务器原来根本不支持eclipselink)

Glassfish, names the JNDI resource as jdbc/website , yet parses it as java:jdbc/website Glassfish将JNDI资源命名为jdbc/website ,但将其解析为java:jdbc/website

Jboss on the other hand, requires the "java:" prefix to be defined explicitly, so in order to adress the same data source, it should be named java:jdbc/website 另一方面,Jboss要求明确定义“ java:”前缀,因此,为了获得相同的数据源,应将其命名为java:jdbc/website

i'm not entirely sure how JPA/Java EE parse the JNDI names internally, but it seems as though either version works, so both java:jdbc/website AND jdbc/website would succesfully connect to the datasource defined in both the glassfish and the jboss environment. 我不完全确定JPA / Java EE是如何在内部解析JNDI名称的,但是似乎这两个版本都可以工作,因此java:jdbc/websitejdbc/website都可以成功连接到glassfish和jboss环境。 at least i've been able to succesfully build to both jboss and glassfish using the java:jdbc/website datasource name, and i've had the same result with jdbc/website 至少我已经能够使用java:jdbc/website数据源名称成功地构建到jboss和glassfish,并且与jdbc/website取得了相同的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JPA多个持久性持久性单元和JTA事务 - JPA multiple persistence persistence units and JTA transaction 具有多个持久性单元的Spring JPA ORM关系 - Spring JPA ORM relationships with multiple persistence units JPA / Hibernate:具有多个持久性单元的模式生成 - JPA / Hibernate : schema generation with multiple persistence units 具有多个持久性单元的Spring Data JPA CDI集成 - Spring Data JPA CDI integration with multiple persistence units 使用 Spring 时如何注入多个 JPA EntityManager(持久化单元) - How to inject multiple JPA EntityManager (persistence units) when using Spring 如何在 JPA 2 应用程序中拥有多个持久性单元? - How to have more than one persistence units in an JPA 2 application? Spring Data JPA - 作为JUnit运行的多个持久性单元,但不在Tomcat中 - Spring Data JPA - Multiple Persistence Units working as JUnit, but not in Tomcat 如何在不同的持久性单元上使用JPA和JUnit进行测试? - How to do tests with JPA and JUnit on differents persistence units? 具有NetBeans的两个JPA持久性单元的表名称相同 - Same table name for two JPA persistence units with NetBeans 具有多个JPA持久性单元的Spring注释配置不持久 - Spring Annotation Config with Multiple JPA Persistence Units Doesn't Persist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM