动态JPA连接 [英] Dynamic JPA Connection

查看:71
本文介绍了动态JPA连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用JPA 2的相当标准的Java EE6 Web应用程序,并且具有依赖关系注入连接到MySQL数据库,并且一切正常.我现在想做的是让该应用程序与我们在客户端站点上安装的其他应用程序的数据库交互-本质上充当我们其他应用程序安装的一个控制点.

I have a fairly standard Java EE6 web application using JPA 2 with dependency injection connecting to a MySQL database and everything is working fine. What I would like to do now is have this application interact with the databases of other applications we have installed at a clients site - essentially acting as a single point of control for our other application installs.

我正在努力的是如何最好地与其他数据库进行交互.理想情况下,我想为每个安装创建一个EntityManager并使用JPA进行交互,但是我看不到任何设置方法.例如,我可能有一种应用程序类型的5个安装(因此还有数据库),而主控制应用程序直到运行时才知道其他安装.这似乎排除了使用EntityManager的依赖项注入以及所有自动事务删除等问题的替代方法.替代选项是仅创建一个DataSource并手动进行交互.尽管很灵活,但这显然需要付出更多的努力.

What I'm struggling with is how best to perform the interaction with the other databases. Ideally I would like to create an EntityManager for each install and interact using JPA but I can't see any way to set this up. I may, for example, have 5 installs (and therefore databases) of one application type and the master control application won't know about the other installs until runtime. This seems to preclude using dependency injection of an EntityManager and all the automatic transaction demacation etc etc. The alternative option is to just create a DataSource and do the interactions manually. While flexible this clearly requires a lot more effort.

所以,我的问题确实是我该如何最好地解决这个问题?

So, my question really is how do I best tackle this problem?

推荐答案

我也在对此进行研究,到目前为止,我已经找到了以下博客文章,其中描述了一种实现方法 http://ayushsuman.blogspot.com/2010 /06/configure-jpa-during-run-time-dynamic.html :

I'm also looking into this, and so far I have found the following blog post that describes a way to do it http://ayushsuman.blogspot.com/2010/06/configure-jpa-during-run-time-dynamic.html :

从persistance.xml中删除了所有数据库属性

Removed all your database properties from persistance.xml

<persistence>
<persistence-unit name="jpablogPUnit" transaction-type="RESOURCE_LOCAL">
<class>com.suman.Company</class>
</persistence-unit>
</persistence>

更改了用于配置entityManager的java文件,在我们的示例中为TestApplication.java

Changed your java file where you are configuring entityManager, in our case TestApplication.java

package com.suman;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.log4j.Logger;

/**
* @author Binod Suman
*/
public class TestApplication {

Logger log = Logger.getLogger(TestApplication.class);
public static void main(String[] args) {
TestApplication test = new TestApplication();
test.saveCompany();
}

public void saveCompany(){
log.info("Company data is going to save");
EntityManagerFactory emf;
Map properties = new HashMap();
properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/sumandb");
properties.put("hibernate.connection.username", "root");
properties.put("hibernate.connection.password", "mysql");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.show-sql", "true");
//emf = Persistence.createEntityManagerFactory("jpablogPUnit");
emf = Persistence.createEntityManagerFactory("jpablogPUnit",properties);
EntityManager entityManager = (EntityManager) emf.createEntityManager();
entityManager.getTransaction().begin();
Company company = new Company(120,"TecnoTree","Espoo, Finland");
entityManager.persist(company);
entityManager.getTransaction().commit();
log.info("Company data has been saved");
}

}

这篇关于动态JPA连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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