JPA - EclipseLink - 如何在运行时配置数据库模式名称 [英] JPA - EclipseLink - How to configure Database Schema name at runtime

查看:184
本文介绍了JPA - EclipseLink - 如何在运行时配置数据库模式名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了使用Oracle数据库的Web应用程序(JEE6,EJB WebProfile)。我的问题是,我需要更改使用的数据库架构(名称)而不重新编译/重新包装应用程序。所以我想要的(这只是一个想法,也许有人有一个更好的一个),是在服务器中有一些配置(JNDI),具体说明了模式名称。但是如何配置Eclipse Link在运行时使用其他架构名称?



详细信息:



使用 orm.xml 文件指定模式名称。但是应用程序使用三种不同的模式名称(一种用于开发,一种用于集成测试,另一种用于生产),因此我需要编译和打包(maven)应用程序3次。



我有一个使用Oracle DB在Glassfish上运行的JEE6 EJB WebProfile应用程序,数据库连接由应用程序服务器处理,并通过JNDI向应用程序提供。


解决方案

您可以使用EclipseLink <一个href =http://www.eclipse.org/eclipselink/api/2.3/org/eclipse/persistence/config/SessionCustomizer.html =nofollow noreferrer> SessionCustomizer 。

  package some.java.package; 

import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.sessions.Session;
import org.eclipse.persistence.sessions.DatabaseLogin;

public class MySessionCustomizer实现SessionCustomizer {

private String schema =some_schema;
public MySessionCustomizer(){
schema = ... //从属性,jndi等读取
}

public void customize(Session session){
session.getLogin()。setTableQualifier(schema);
}
}


I implement a Web Application (JEE6, EJB WebProfile) that uses an Oracle DB. My Problem is, that I need to change the used Database Schema (name) without recompile/repackage the application. So what I want (this is only an idea, maybe someone has an better one), is to have some configuration (JNDI) within the Server, that specifics the Schema name. But how configure Eclipse Link to use an other schema name at runtime?

Details:

At the moment I use the orm.xml file to specify the Schema name. But the Application uses three different Schema names (one for development, one for integration test, and one for production), so I need to compile and package (maven) the application 3 times.

I have a JEE6 EJB WebProfile Application running on an Glassfish with using an Oracle DB and the Database Connection is handled by the Application Server and provieded to the Application via JNDI.

Does any body has an idea how to configure the database schema name at runtime.

解决方案

You can use an EclipseLink SessionCustomizer.

package some.java.package;

import org.eclipse.persistence.config.SessionCustomizer; 
import org.eclipse.persistence.sessions.Session; 
import org.eclipse.persistence.sessions.DatabaseLogin; 

public class MySessionCustomizer implements SessionCustomizer {

  private String schema = "some_schema";
  public MySessionCustomizer() {
      schema = ... // read from property, jndi, etc.
  }

  public void customize(Session session) { 
      session.getLogin().setTableQualifier(schema);
  } 
}

这篇关于JPA - EclipseLink - 如何在运行时配置数据库模式名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆