我也在对此进行研究,到目前为止,我已经找到了以下博客文章,该文章描述了一种实现方法
: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;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");}}


