动态 JPA 连接

发布于 2024-09-30 10:58:29 字数 392 浏览 4 评论 0原文

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

我正在努力解决的是如何最好地执行与其他数据库的交互。理想情况下,我想为每次安装创建一个 EntityManager 并使用 JPA 进行交互,但我看不到任何设置方法。例如,我可能对一种应用程序类型进行了 5 次安装(因此也有数据库),并且主控制应用程序在运行时之前不会知道其他安装。这似乎排除了使用 EntityManager 的依赖注入和所有自动事务划分等。替代选项是仅创建一个数据源并手动进行交互。虽然灵活,但这显然需要付出更多努力。

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2024-10-07 10:58:29

我也在研究这个,到目前为止我发现了以下博客文章描述了一种方法
http://ayushsuman.blogspot.com/2010 /06/configure-jpa-during-run-time-dynamic.html

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

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

更改了配置entityManager的java文件,在我们的例子中是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");
}

}

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 :

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>

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");
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文